mysql使用连表查询,如何取相反值?


select * from a left join b on a.id=b.id  

类似这种sql查询出来的数据 比如是他们是显示共同有的数据通过id。如何查询出a表中的数据在b表中不存在的数据?

大家为啥都喜欢not in????
select *
from a
left join b on a.id=b.id
where b.id is null

select * from a where id not in (select id from b) 

使用not in 查一下
select * from A where id not in(select a.id from A a inner join B b on a.id =b.id)
select * from B where id not in(select a.id from A a inner join B b on a.id =b.id)

但是这样查效率会比较低


select * from a where id not in (select id from b)

做成条件呗
select * from a. where id not in (select id from b)

用exists效率比in高


select * from a where not exists (select 1 from b where b.id = a.id);

一般情况下,用exists更好
select * from a where not exists (select 1 from b where a.id = b.id)