Sql 使用 NOT LIKE IN WHERE 子句进行查询
Sql Query with NOT LIKE IN WHERE CLAUSE
我有两个 tables emails 和 blockedSender 我正在尝试从电子邮件查询中删除被阻止的发件人。例如
电子邮件table:
user_id
from_address
1
name-1 <email-1@address.com>
2
name-2 <email-2@address.com>
阻止发件人table:
blocked_address
email-1@address.com
这里我想 return 电子邮件 table 中的所有元素,其中 from_address 不与被阻止地址中的条目进行 LIKE 匹配。类似于
select email_id, from_address
from emails e
where from_address not like in (select '%' + blocked_address + '%'
from blockSenders)
由于语法不正确,它不起作用。有什么办法吗?
可以改写为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;
您可以使用不存在
来表达
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 + '%'
)
我有两个 tables emails 和 blockedSender 我正在尝试从电子邮件查询中删除被阻止的发件人。例如
电子邮件table:
user_id | from_address |
---|---|
1 | name-1 <email-1@address.com> |
2 | name-2 <email-2@address.com> |
阻止发件人table:
blocked_address |
---|
email-1@address.com |
这里我想 return 电子邮件 table 中的所有元素,其中 from_address 不与被阻止地址中的条目进行 LIKE 匹配。类似于
select email_id, from_address
from emails e
where from_address not like in (select '%' + blocked_address + '%'
from blockSenders)
由于语法不正确,它不起作用。有什么办法吗?
可以改写为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;
您可以使用不存在
来表达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 + '%'
)