我有3个表队,球员和TxP
Teams表包含TeamName和TeamID(主键)列 玩家表有列PlayerName和PlayerID(主键) TxP表的列为PlayerID,TeamID
我想编写一个查询来将结果集设置为PlayerName,TeamName
答案 0 :(得分:1)
SELECT Players.PlayerName, Teams.TeamName
FROM Players
LEFT JOIN TxP ON Players.PlayerID = TxP.PlayerID
LEFT JOIN Teams ON TxP.TeamID = Teams.TeamID
ORDER BY Players.PlayerName
这将为每个玩家和团队组合提供一行,如果玩家没有团队,则包括一个空TeamName的行。
仅显示有球队的球员只是切换到不使用左连接。
例如,这可能会给出:
Bob Sample United
Bob Some other team
Chris
如果使用普通(内部)连接,则无法获得Chris结果。如果玩家可以拥有多个团队,但您只需要一个结果,则需要GROUP BY和聚合函数将您的团队名称分组为单个值。
答案 1 :(得分:0)
select Teams.TeamName, TxP.PlayerID
from Teams
right outer join TxP on TxP.TeamID = Teams.TeamID
答案 2 :(得分:0)
SELECT Players.PlayerName, Teams.TeamName FROM Players, Teams, TxP
WHERE Teams.TeamID = TxP.TeamID AND Players.PlayerID = TxP.PlayerID
答案 3 :(得分:0)
SELECT A.PlayerName,B.TeamName FROM Players A,Teams B,TxP C WHERE A.PlayerID = C.PlayerID AND B.TeamID = C.TeamID
此查询仅显示分配给至少一个团队的玩家和至少有一个玩家的团队
答案 4 :(得分:0)
从Player,Team,TXP中选择Player.PlayerName,Team.TeamName,其中Team.TeamId = TXP.TeamId和 Player.PlayerId = TXP.PlayerId