比较两个SQL表并返回缺少的ID? [英] Compare two SQL tables and return missing ids?
本文介绍了比较两个SQL表并返回缺少的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个简单的表:(这里只有"id"列)
I have two simple tables: (here only the "id" column)
表1:
id
1
2
3
4
表2:
id
2
4
sql查询应该比较两个表中table2中缺少的"id"并返回: 1,2
the sql query should compare the two tables for missing "id" in table2 and return: 1,2
有什么想法吗? :) TY
any ideas? :) TY
推荐答案
有几种方法可以给这只猫换皮:
There are several ways to skin this cat:
SELECT table1.ID
FROM table1
WHERE table1.ID NOT IN(SELECT table2.ID FROM table2)
或者您可以使用左外部联接:
Or you could use a left outer join:
SELECT table1.ID
FROM table1
LEFT OUTER JOIN table2 ON table1.ID = table2.ID
WHERE table2.ID IS NULL
这篇关于比较两个SQL表并返回缺少的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文