您的位置:首页 > 其它

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

2011-12-07 13:48 441 查看
例如:两个表: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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐