查找一个表中存在而另一个表中不存在的记录

186 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

例如:两个表:t1, t2 ,查询在表t1中存在,而在表t2中不存在的记录。 假设以id字段为关联字段。

方法1:需要两个表的字段完全一致

select *  

from t1

minus  (oracle数据库中才有)

selecct * from t2

 

方法2:

select * from t1

where not exists(select 1 from t2 where t1.id=t2.id)

 

方法3:

select * from t1

where id not in(select id from t2)

 

方法4:需要t2.id不能为空

select t1.* 

from t1

left join t2 on t1.id=t2.id 

where t2.id is null