如何使用JOIN语句使NOT IN工作? 以下是我的查询:
SELECT A.*
FROM products A
LEFT OUTER JOIN productmeta B ON A.id=B.product_id
WHERE(B.group_id IS NULL OR B.group_id=0)
AND (B.collection_id IS NULL OR B.collection_id=0)
AND B.id NOT IN (SELECT collection_id FROM collections)
答案 0 :(得分:2)
我认为问题是第三个条款。 NULL
总会导致它失败。但这很容易测试:
SELECT A.*
FROM products A LEFT OUTER JOIN
productmeta B
ON A.id=B.product_id
WHERE (B.group_id IS NULL OR B.group_id=0) AND
(B.collection_id IS NULL OR B.collection_id=0) AND
(B.ID is NULL or B.id NOT IN (SELECT collection_id FROM collections));
虽然我没有更改它们,p
会为products
提供更好的别名,pm
是productmeta
更好的别名。