情况如下: -
我想加入2个表。让我们说表a
和b
SELECT *
FROM a
JOIN b ON a.id = b.id AND b.status = '1'
问题在于:
b.status = '1'
只应在时添加
b.stage in (1, 3, 5, 6, 8)
如何在ON
子句中添加此类条件?
喜欢
ON a.id = b.id
CASE
IF (b.stage in (1, 3, 5, 6, 8))
THEN
AND b.status = '1'
END
答案 0 :(得分:5)
您的状况在逻辑上与" 阶段不在列表中或状态为1":
SELECT *
FROM a
JOIN b ON a.id = b.id
AND (b.stage not in (1, 3, 5, 6, 8) OR b.status = '1')