[英]Sql Query with NOT LIKE IN WHERE CLAUSE
I have two tables emails and blockedSender I am trying to remove blocked sender from the email query.我有两个表emails和blockedSender我正在尝试从 email 查询中删除被阻止的发件人。 eg
例如
emails table:电子邮件表:
user_id![]() |
from_address![]() |
---|---|
1 ![]() |
name-1 <email-1@address.com> ![]() |
2 ![]() |
name-2 <email-2@address.com> ![]() |
blockedSender table:阻止发件人表:
blocked_address![]() |
---|
email-1@address.com ![]() |
Here I want to return all elements from the emails table where the from_address does not do a LIKE match with entries in the blocked address.在这里,我想返回 emails 表中的所有元素,其中 from_address 不与被阻止地址中的条目进行 LIKE 匹配。 Something similar to
类似的东西
select email_id, from_address
from emails e
where from_address not like in (select '%' + blocked_address + '%'
from blockSenders)
which does not work because of incorrect syntax.由于语法不正确,这不起作用。 Is there any way to do this?
有没有办法做到这一点?
You can express this using not exists您可以使用不存在来表达这一点
select e.email_id, e.from_address
from emails e
where not exists (
select * from blockedSenders b
where e.from_address like '%' + b.blocked_address + '%'
)
It could be rewritten as JOIN
:它可以重写为
JOIN
:
select e.email_id, e.from_address
from emails e
left join blockSenders b
ON e.from_address LIKE '%' + b.blocked_address + '%'
where b.blocked_address IS NULL;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.