Losing a right-joined row when doing a NOT IN
I have three tables:
email_addr_bean_rel
bean_id | email_address_id
email_addresses
id | email_address
prospect_lists_prospects
id | related_id
The above is a huge simplification to demonstrate the issue. What I need is a list of all related_ids and all available email addresses. But I have to start the query on the email_addresses table. So I'm using a RIGHT JOIN to get any related_ids that don't have e开发者_Go百科mail addresses. Here is the query:
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")
If I take out the WHERE clause, I get all related ids and the email_address is set to NULL for the ones that don't match up (as expected). But when I add in the WHERE clause, I lose those RIGHT JOINED records.
Is there some aspect of NOT IN
or RIGHT JOIN
that I'm missing that would cause this?
ea.email_address
is NULL. But NULL NOT IN ("x", "y", "z")
always evaluates to NULL.
I think you need this:
WHERE (ea.email_address is NULL) or (ea.email_address NOT IN ("x", "y", "z"))
精彩评论