我有两个简单的表:(这里只有“id”列)
表1:
id
1
2
3
4
表2:
id
2
4
sql查询应比较表2中缺少“id”的两个表并返回: 1,2
任何想法? :) TY
答案 0 :(得分:30)
有几种方法可以遮盖这只猫:
SELECT table1.ID
FROM table1
WHERE table1.ID NOT IN(SELECT table2.ID FROM table2)
或者您可以使用左外连接:
SELECT table1.ID
FROM table1
LEFT OUTER JOIN table2 ON table1.ID = table2.ID
WHERE table2.ID IS NULL
答案 1 :(得分:2)
select t1.*
from table1 t1
left outer join table2 t2 on t1.id = t2.id
where t2.id is null
答案 2 :(得分:1)
试试这个:
SELECT table1.id
FROM table1
WHERE table1.id NOT IN(SELECT table2.id FROM table2)