使用NOT IN和JOIN语句

时间:2013-05-24 02:34:21

标签: mysql join

如何使用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)

1 个答案:

答案 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提供更好的别名,pmproductmeta更好的别名。