This question already has answers here:
Return row only if value doesn't exist
(2个答案)
5年前关闭。
assignments:
id | prospectid
1  | 1
2  | 2
3  | 5

prospects:
id | name
1  | purple
2  | red
3  | blue
4  | orange
5  | green

我想返回一个前景列表,其中ID不存在于任务范围内的PosithID栏中。简而言之,我想返回未分配的潜在客户。
因此,从上面的例子来看,它应该返回:
prospects:
3 | blue
4 | orange

我失败的SQL:
SELECT * FROM prospects
WHERE prospects.id != `assignments`.prospectid

最佳答案

可以使用NOT EXISTS子句

select *
from prospects p
where not exists (select null from
                  assignments a
                  where a.prospectid = p.id)

07-25 21:10