我有一个问题:
SELECT matches.id,
matches.player1,
matches.player2,
users.firstname as firstname,
tournaments.tid as tid,
users.tempsalt as salt
FROM matches
INNER JOIN tournaments
ON matches.tid = tournaments.tid
INNER JOIN users
ON matches.uid = users.uid
WHERE ((matches.status = 0)
OR (matches.status = 1))
AND (tournaments.status <> 3)
AND (users.tempsalt = '324234324234')
三张桌子 - 匹配,锦标赛,用户
加了: 比赛[1,3,2,约翰·马克,0] 并[2,3,NULL,Piter酒店,萨拉,1]
用户[1,3,亚历克斯,346] [2,4,萨姆,32423]
比赛[1,3,2] 我想得到结果: [1,约翰,马克,山姆,3,32423],也有NULL [2,Piter,Sara,NULL,3,NULL]
如果matches.uid为NULL,则无结果。但是我想在matches.uid也是NULL时得到结果。可以在一个SQL查询中使用吗?
答案 0 :(得分:2)
使用left join
并将联接表的条件放在on
子句中
SELECT m.id, m.player1, m.player2,
u.firstname as firstname, u.tempsalt as salt,
t.tid as tid
FROM matches m
LEFT JOIN tournaments t ON m.tid = t.tid
AND t.status <> 3
LEFT JOIN users u ON m.uid = u.uid
AND u.tempsalt = '324234324234'
WHERE m.status in (0,1)