如何在SQLite中从表中获得值,而不从表中获得值?

时间:2019-09-23 18:49:35

标签: sqlite

我有两个表: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;

有可能吗?

1 个答案:

答案 0 :(得分:1)

您可以将UNION ALL用于表TeamPlayerPlayerTeamPlayer中不存在的行:

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列的不存在列将为0Active
您可以根据需要进行更改。