mysql中的IF语句JOIN - 条件成功时添加多个条件

时间:2013-03-14 12:15:37

标签: mysql

情况如下: -

我想加入2个表。让我们说表ab

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

1 个答案:

答案 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')