在进行NOT IN时丢失右连接的行

时间:2011-07-12 15:03:10

标签: mysql join

我有三张桌子:

email_addr_bean_rel

    bean_id  |  email_address_id

email_addresses

    id       |  email_address

prospect_lists_prospects

    id       |  related_id

以上是一个很大的简化来证明这个问题。我需要的是所有related_ids和所有可用电子邮件地址的列表。但我必须在email_addresses表上启动查询。所以我正在使用RIGHT JOIN来获取任何没有电子邮件地址的related_ids。这是查询:

SELECT 
       plp.related_id,
       ea.email_address
FROM   email_addresses AS ea 
       JOIN email_addr_bean_rel AS eabr 
         ON ea.id = eabr.email_address_id 
       RIGHT JOIN prospect_lists_prospects AS plp 
         ON eabr.bean_id = plp.related_id 
WHERE
      ea.email_address NOT IN ("x", "y", "z")

如果我取出WHERE子句,我得到所有相关的id,并且对于那些不匹配的(如预期的那样),email_address被设置为NULL。但是当我添加WHERE子句时,我丢失了那些RIGHT JOINED记录。

我遗漏了NOT INRIGHT JOIN的某些方面会导致这种情况吗?

1 个答案:

答案 0 :(得分:2)

ea.email_address NULL 。但NULL NOT IN ("x", "y", "z")始终评估为 NULL

我认为你需要这个:

WHERE (ea.email_address is NULL) or (ea.email_address NOT IN ("x", "y", "z"))