我需要加入OR条件

时间:2015-10-07 19:10:30

标签: mysql join

所以我有一个加入,例如,让我们说

SELECT a.id FROM tableA as a 
  JOIN tableB as b on a.id=b.aid 
  JOIN tableC as c on (c.aid != a.id or ???) and a.name = 'fedex' and b.user = 'employee' and c.enabled=true

???如果c不包含那个a.id我仍然希望结果包含该结果

我尝试过左外连接但是没有做到这一点

顺便说一句,即使我在表c中有一条记录,这个查询也能正常工作。如果表c为空,则查询失败时为空。

2 个答案:

答案 0 :(得分:1)

您正在描述LEFT JOIN的作用。所以,你试过这个吗?

SELECT a.id
FROM tableA a JOIN
     tableB b
     on a.id = b.id LEFT JOIN
     tableC c
     on c.id = a.id;

当然,您的查询非常简单,以至于您甚至不需要TableC。这将返回同一组a.id

SELECT a.id
FROM tableA a
WHERE a.id IN (SELECT b.ID FROM tableB b)

唯一的区别是您的版本可能会产生重复。

答案 1 :(得分:0)

SELECT a.id FROM tableA as a 
JOIN tableB as b on a.id = b.aid and b.user = 'employee' 
LEFT JOIN tableC as c on c.aid != a.id and a.name = 'fedex' and c.enabled=true