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

霸姨

关注

阅读 14

2023-10-12

例如:两个表:t1, t2 ,查询在表t1中存在,而在表t2中不存在的记录。


         假设以id字段为关联字段。


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


select *

from t1

minus

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


精彩评论(0)

0 0 举报