假设表TABLE1的A列为1,3,5 表TABLE2的B列为1,2,4,5
NOT EXISTS写法:
SELECT T1.A
FROM TABLE1 T1
WHERE NOT EXISTS(SELECT 1 FROM TABLE2 T2
WHERE T1.A = T2.B)
NOT IN写法:
SELECT T1.A
FROM TABLE1 T1
WHERE T1.A NOT IN(SELECT T2.B FROM TABLE2 T2)
小表建议使用NOT IN
假设表table1,列a,表table2,列b
select a from table1
where a not in
(
select b from table2
)
使用左链接吧!
select a.* from 表1 as a left join 表2 as b on a.id = b.id and b.id is null
select t1.columnA from table1 t1 where not exists (select 0 from table2 t2 where t2.columnA = t1.columnA)