我有三张桌子:
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 IN
或RIGHT JOIN
的某些方面会导致这种情况吗?
答案 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"))