表a 表b 查询在a中但不在b中的数据
a、b表,找出id字段中,存在a表,但是不存在b表的数据。
方法一:
子查询统计的是关联上的记录,等于0就是关联不上,就不存在(效率最好)
select * from a where (select count(1) as num from b where b.id = a.id) = 0
方法二:
select distinct a.id from a where a.id not in (select id from b);
方法三:
left join…on… , “b.id isnull” 表示左连接之后在b.id 字段为 null的记录
select a.id from a left join b on a.id=b.id where b.id is null