查找一個表中存在而另一個表中不存在的記錄

例如:兩個表: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

發佈了139 篇原創文章 · 獲贊 21 · 訪問量 82萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章