int char int int
id name a_id b_id
SELECT count(*) FROM tbl t1 WHERE b_id = 12 AND NOT EXISTS(select * from tbl t2 where t2.a_id = t1.b_id AND t2.b_id = t1.a_id)
我認爲它至少等於
SELECT count(*) FROM tbl t1 WHERE b_id = 12 AND NOT EXISTS(select * from tbl t2 where t2.a_id = 12 AND t2.b_id = t1.a_id)
那麼這意味着什麼表?
例如,SELECT a_id FROM tbl t1 WHERE b_id = 12
給1,2,3,4
然後執行以下操作:
select * from tbl t2 where t2.a_id = 12 AND t2.b_id = 1 # NULL
select * from tbl t2 where t2.a_id = 12 AND t2.b_id = 2 # exists
select * from tbl t2 where t2.a_id = 12 AND t2.b_id = 3 # exists
select * from tbl t2 where t2.a_id = 12 AND t2.b_id = 4 # NULL
所以COUNT(*)將是2?
它與NULL的LEFT JOIN一樣,這是我們如何做到這一點 – Strawberry
對不起,我是sql新手,你能用描述性的方式解釋嗎? – Sato
那麼它選擇查詢的一部分中沒有出現在查詢的第二部分的東西 – Strawberry