assignments:
id | prospectid
1 | 1
2 | 2
3 | 5
prospects:
id | name
1 | purple
2 | red
3 | blue
4 | orange
5 | green
我想要返回一个潜在客户列表,其中在指配栏中的prospectid列下不存在prospect.id。简而言之,我想回到未分配的潜在客户。
所以从上面的例子中,它应该返回:
prospects:
3 | blue
4 | orange
我失败的SQL:
SELECT * FROM prospects
WHERE prospects.id != `assignments`.prospectid
答案 0 :(得分:3)
您可以使用NOT EXISTS条款
select *
from prospects p
where not exists (select null from
assignments a
where a.prospectid = p.id)
答案 1 :(得分:1)
使用LEFT JOIN
SELECT P.id, P.name
FROM Prospects P
LEFT JOIN assignments A
on P.id = A.prospectid
WHERE A.prospectid is NULL