在我有两张桌子的情况下:
我想选择所有游戏和玩家的性别,我如何绕过null playerIds?
从this explanation开始,我认为答案是:
SELECT Games.id, p1.gender, p2.gender
FROM Games JOIN Players p1 on player1Id = p1.id
JOIN Players p2 on player2Id = p2.id
但是,这不会返回任何将player1Id或player2Id设为null的游戏。有没有办法让玩家在可能的情况下连接的所有游戏?
感谢。
答案 0 :(得分:5)
您需要使用LEFT JOIN:
SELECT g.id, p1.gender, p2.gender
FROM Games g
LEFT JOIN Players p1 ON p1.id = g.player1Id
LEFT JOIN Players p2 on p2.id = g.player2Id
这是really good link that visual demonstrates the different JOINs