SQL Check one list against Another

我与影子孤独终老i 提交于 2020-01-06 04:53:30

问题


I'd appreciate any pointers on how in SQL to check whether elements in one list also appear in another.

List A = Live Customers in April
List B = Live Customers in May

How can I check which Customers in List A also appear in List B ? to identify those Customers which have been lost

i.e. Customers in A but not in B.

Thank you for your help. Gav


回答1:


Different ways to pull the results

SELECT customer
FROM ListA a 
WHERE NOT EXISTS (SELECT 1 FROM ListB b WHERE a.customer=b.customer)

OR

SELECT a.customer
FROM ListA a 
  LEFT JOIN ListB b ON a.customer=b.customer
WHERE b.customer is null

OR

SELECT customer
FROM ListA

except

SELECT customer
FROM ListB

OR

SELECT customer
FROM ListA 
WHERE customer NOT IN (SELECT customer FROM ListB )



回答2:


Try the not in clause

example

select * 
from mytable 
where id not in (select id from table2)

this will return results that are not in another table. quick and simple



来源:https://stackoverflow.com/questions/39001651/sql-check-one-list-against-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!