我有两个表:Player(名称,电子邮件)和TeamPlayer(PlayerName,团队,活动)。表Player和TeamPlayer通过Player.name = TeamPlayer.PlayerName连接。
我想获取TeamPlayer的所有元素,但还要获取Player中的元素,而不是TeamPlayer中的元素。属性Active也很重要。我执行下一条命令,但是没有用:
SELECT DISTINCT Player.Name, TeamPlayer.Team, TeamPlayer.Active FROM Player LEFT JOIN TeamPlayer ON TeamPlayer.PlayerName=Player.Name ORDER BY TeamPlayer.Active;
有可能吗?
答案 0 :(得分:1)
您可以将UNION ALL
用于表TeamPlayer
和Player
中TeamPlayer
中不存在的行:
SELECT PlayerName AS Name, Team, null AS email, Active
FROM TeamPlayer
UNION ALL
SELECT t.Name, null, t.email, 0
FROM Player t
WHERE NOT EXISTS (
SELECT 1 FROM TeamPlayer
WHERE PlayerName = t.Name
)
ORDER BY Active
由于2个表没有相同的列,因此对于每种情况,NULL
列的不存在列将为0
或Active
。
您可以根据需要进行更改。