我正在尝试在使用mysql“NOT IN”条件时离开连接2个表。我遇到的问题是,如果left join为null,意味着没有左连接的条目,则mysql NOT IN运算符不能正常工作。
这是一个例子。
SELECT PLAYERS.*,
TYPES.GUEST_ID
FROM PLAYERS
LEFT JOIN TYPES ON TYPES.GUEST_ID = PLAYERS.GUEST_ID
WHERE PLAYERS.ID = '1' AND TYPES.GUEST_ID NOT IN('1', '2')
由于TYPES表中当前没有匹配的行。 NOT IN运算符不起作用。有没有解决方法呢?
答案 0 :(得分:1)
使用IS NULL
:
SELECT PLAYERS.*, TYPES.GUEST_ID
FROM PLAYERS
LEFT JOIN TYPES ON TYPES.GUEST_ID = PLAYERS.GUEST_ID
WHERE TYPES.GUEST_ID NOT IN('1', '2') OR TYPES.GUEST_ID IS NULL
答案 1 :(得分:0)
将其放入on
子句
SELECT PLAYERS.*,
TYPES.GUEST_ID
FROM PLAYERS
LEFT JOIN TYPES ON TYPES.GUEST_ID = PLAYERS.GUEST_ID
AND TYPES.GUEST_ID NOT IN(1, 2)