我很感激有关如何在SQL中检查一个列表中的元素是否也出现在另一个列表中的指示。
List A = Live Customers in April
List B = Live Customers in May
如何查看列表A中哪些客户也出现在列表B中?识别那些已经失去的客户
i.e. Customers in A but not in B.
感谢您的帮助。 GAV
答案 0 :(得分:2)
提取结果的不同方法
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 )
答案 1 :(得分:0)
尝试not in
子句
示例
select *
from mytable
where id not in (select id from table2)
这将返回不在另一个表中的结果。快速简单