在oracle中执行select时无效的标识符

时间:2016-12-04 21:12:49

标签: sql oracle select join sqlplus

我正在尝试在oracle中执行一个使用3个表,nba_player,nba_team和nba_team_roster的select。 nba_player包含带有player_id的玩家数据,nba_team包含带有team_id的团队信息。 nba_team_roster是一个关联实体,包括一个player_id和team_id来关联这两个实体。我希望这个查询返回团队'OKC'上每个玩家的名字和姓氏,但由于某种原因,它给了我下面的错误。我不确定为什么这不能正常执行。任何帮助将不胜感激。

select nba_player.first_name, nba_player.last_name
from nba_player,nba_team
join nba_team_roster
on nba_team_roster.player_id=nba_player.player_id
where nba_team_roster.team_id= nba_team.team_id
and nba_team.team_name='OKC';


on nba_player.player_id=nba_team_roster.player_id
   *
ERROR at line 4:
ORA-00904: "NBA_PLAYER"."PLAYER_ID": invalid identifier

CREATE TABLE NBA_Team(
team_id number primary key,
team_name varchar(5)
);

CREATE TABLE NBA_Player(
player_id number primary key,
first_name varchar(10),
last_name varchar (11),
position varchar(3),
salary number,
points_per_game number
);

CREATE TABLE NBA_Team_Roster(
roster_ID number primary key,
team_id number,
player_id number unique,
foreign key (team_id) references NBA_Team(team_id),
foreign key (player_id) references NBA_Player(player_id)
);

1 个答案:

答案 0 :(得分:2)

简单规则:从不FROM子句中使用逗号。 始终使用正确的,明确的JOIN语法。这将解决您的问题并使查询更容易理解:

select p.first_name, p.last_name
from nba_player p join
     nba_team_roster r
     on r.player_id = p.player_id join
     nba_team t
     on r.team_id = t.team_id
where t.team_name = 'OKC';

请注意,我还介绍了表别名。这些使查询更容易编写和阅读。