【问题标题】:PL/SQL Trying to display tableID as another tables NamePL/SQL 试图将 tableID 显示为另一个表名称
【发布时间】:2020-02-06 22:32:40
【问题描述】:

我正在制作一个 NHL oracle sql 数据库。我正在尝试创建一个 SELECT 查询,它显示来自游戏表的 homeTeam,但显示来自团队表的 teamName。本质上,我希望输出为“SELECT hometeam, homecore, awayteam, awayscore”,但我不想显示 teamID,而是希望在 homeTeam 和 awayTeam 列中看到 teamNames。我相信这可以通过加入来完成,只是无法使其正常工作。

create table team (
  teamID        number(2,0) not null,
  teamAbb       varchar2(3),
  teamName      varchar2(20),
  location      varchar2(20),
  division      varchar2(20),
  conference    varchar2(20),
  constraint pk_team primary key (teamID)
);

create table game(
 gameID     number(2,0) not null,
 gameDate   date,
 homeTeam   number(2,0),
 homeScore  number(2,0),
 awayTeam   number(2,0),
 awayScore  number(2,0),
 constraint pk_game primary key (gameID),
 constraint fk_homeTeam foreign key (homeTeam) references team (teamID),
 constraint fk_awayTeam foreign key (awayTeam) references team (teamID)
);

插入这些

DECLARE

BEGIN

insert into team values(10, 'ANA', 'Ducks', 'Anaheim', 'Pacific', 'Western');
insert into team values(11, 'ARI', 'Coyotes', 'Arizona', 'Pacific', 'Western');
insert into team values(12, 'BOS', 'Bruins ', 'Boston', 'Atlantic', 'Eastern');
insert into team values(13, 'BUF', 'Sabers ', 'Buffalo', 'Atlantic', 'Eastern');

COMMIT;

END;

DECLARE

BEGIN

insert into game values (1, to_date('01-11-2020','mm-dd-yyyy'), 10, 2, 11, 1);
insert into game values (2, to_date('01-13-2020','mm-dd-yyyy'), 11, 5, 12, 6);
insert into game values (3, to_date('03-04-2020','mm-dd-yyyy'), 10, null, 13, null);

COMMIT;

END;

这是我目前使用 SELECT 看到的内容

【问题讨论】:

    标签: sql oracle plsql


    【解决方案1】:

    您将必须与团队表执行 2 次联接

    查询

    select t1.teamName AS HomeTeam, g.homeScore, t2.teamName AS AwayTeam, g.awayScore
    from    game g
        inner join team t1 ON t1.teamID = g.homeTeam
        inner join team t2 ON t2.teamID = g.awayTeam
    

    结果

    HomeTeam    HomeScore    AwayTeam   AwayScore
    ---------------------------------------------
    Ducks       2            Coyotes    1
    Coyotes     5            Bruins     6
    Ducks       NULL         Sabers     NULL
    

    【讨论】:

      【解决方案2】:

      我认为你只需要两个连接:

      select th.teamname as hometeam, g.homescore,
             ta.teamname as awayteam, g.awayscore
      from games g join
           teams th
           on g.hometeam = th.teamid join
           teams ta
           on g.awayteam = ta.teamid;
      

      【讨论】:

        【解决方案3】:

        你可以这样做:

        select t.teamName "Home team"
               , homescore "Home score"
               , (select teamname from team where teamID = g.awayTeam) "Away team"
               , awayscore "Away score"
        from team t 
        join game g
        on t.teamid = g.hometeam;
        

        P.S:您还可以为列添加别名(别名是列的名称 将显示在结果中)

        Here you can see a demo

        当运动结果有问题时,您也可以这样显示:

        select t.teamName || '-' ||(select teamname from team where teamID = g.awayTeam) "HOME - AWAY"
               , NVL(homescore, 0) || ':' || NVL(awayscore,0) "SCORE"
        from team t 
        join game g
        on t.teamid = g.hometeam
        

        您将在哪里连接团队名称和结果,并且 NVL NULL 值将在结果中转换为 0

        Here is a DEMO 表示该选项。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-12-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多