表1
id,userid,eventid,name
表2
eventid,zoneid,userid
表3
eventid,userid,status
如果所有三个表都具有eventid意味着我不想选择该记录(我的意思是如果表3具有eventid),否则我需要选择记录
我尝试了我的查询
SELECT
*
FROM
`table1` c1
INNER JOIN `table2` c2 ON c2.eventid = c1.eventid
LEFT JOIN table3 c3 ON c3.eventid = c1.eventid
WHERE
c2.zoneid=2
AND c1.active='1'
GROUP BY
c1.eventid
答案 0 :(得分:3)
添加一个where子句,其中没有c3:
SELECT *
FROM `table1` c1
INNER JOIN `table2` c2 ON c2.eventid = c1.eventid
LEFT JOIN table3 c3 ON c3.eventid = c1.eventid
WHERE c2.zoneid=2 AND c1.active='1'
AND c3.id IS NULL
group by c1.eventid
答案 1 :(得分:1)
SELECT
*
FROM
`table1` c1
INNER JOIN `table2` c2 ON c2.eventid = c1.eventid
WHERE
c2.zoneid=2
AND c1.active='1'
AND NOT EXISTS (SELECT * FROM table3 c3 WHERE c3.eventid = c1.eventid)
GROUP BY
c1.eventid
在已通过LEFT / RIGHT OUTER JOIN连接的表上应用WHERE条件(如其他一些建议的建议)实际上会使其成为常规连接。
已发布的其他示例要求c1.eventid等于c3.eventid,并且c3.eventid为NULL - 很有可能结果不符合您的预期,具体取决于数据库如何处理c1.eventid = c3.eventid
两者是NULL(我必须阅读)。
答案 2 :(得分:0)
第3个表上的左连接,条件WHERE C.eventid IS NULL
应该可以正常工作。
SELECT *
FROM table1 A
INNER JOIN table2 B
ON A.eventid = B.eventid
LEFT OUTER JOIN table3 C
ON A.eventid = C.eventid
WHERE C.eventid IS NULL