[英]Reject row from select if condition exists in another table
I have two tables.我有两张桌子。
So one contact maybe have multiple event opt-outs but what I really care about is if they have a company wide opt out.所以一个联系人可能有多个事件选择退出,但我真正关心的是他们是否有一个公司范围的选择退出。
How do I join the data so that if a contact has a match with any row in the optout table that has a scope of "company" it will not show up in the result?如何加入数据,以便如果联系人与 optout 表中具有“公司”的 scope 的任何行匹配,则它不会显示在结果中?
SELECT c.*
FROM Contacts c
WHERE NOT EXISTS(SELECT NULL
FROM OptOut o
WHERE c.contact_email = o.contact_email
AND o.scope = 'company')
This could also be done with a LEFT JOIN
:这也可以通过
LEFT JOIN
来完成:
SELECT c.*
FROM Contacts c
LEFT JOIN OptOut o
ON c.contact_email = o.contact_email
AND o.scope = 'company'
WHERE o.contact_email IS NULL
select * from contacts where contact_email not in (select contact_email from optout where scope = 'company'); select * 来自contact_email 不在的联系人(从optout 中选择contact_email,其中scope = 'company');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.