如何在查询中连接来自不同表的相同名称字段

问题描述 投票:0回答:1

以下代码有效,但是我有一个tblRecPlayers,其中包括1个需要包含在查询中的播放器。 tblRecplayers还包括PlayerID,PlayerFirst和PlayerLast字段,如何将tblRecPlayers和tblCompPlayers结合在一起?

select 
  TeamName as 'Team Name',
  PlayerFirst as 'First Name',
  PlayerLast as 'Last Name', 
  tblCompPlayers.PlayerID
from 
  tblCompPlayers inner join 
  tblTeamRoster inner join 
  tblTeams on tblTeamRoster.TeamID = tblTeams.TeamID
  on tblCompPlayers.PlayerID = tblTeamRoster.PlayerID
where 
  TeamName = 'Avalanche';
sql ssms
1个回答
0
投票

我认为您可能很接近...没有表结构...这只是一个猜测。但是看来您必须移动第二个ON子句。

select 
  TeamName as 'Team Name',
  PlayerFirst as 'First Name',
  PlayerLast as 'Last Name', 
  tblCompPlayers.PlayerID
from 
  tblCompPlayers inner join 
  tblTeamRoster on tblCompPlayers.PlayerID = tblTeamRoster.PlayerID inner join  
  tblTeams on tblTeamRoster.TeamID = tblTeams.TeamID
where 
  TeamName = 'Avalanche';

请注意对于要加入的每个其他表重复的语法:

INNER JOIN table2 ON table1.column_name = table2.column_name

© www.soinside.com 2019 - 2024. All rights reserved.