简体   繁体   English

如果条件存在于另一个表中,则拒绝来自 select 的行

[英]Reject row from select if condition exists in another table

I have two tables.我有两张桌子。

  1. Contacts table - contact_id, contact_email, contact_name, etc联系人表 - contact_id、contact_email、contact_name 等
  2. Opt out table - contact_email, scope of their opt out (event, company)退出表 - contact_email, scope 的退出(事件,公司)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM