in和exists两者到底有什么区别
当B表的数据集小于A表数据集时,用in优于exists。
select id from A where id in (select id from B)
当A表的数据集小于B表的数据集时,用exists优于in。
select id from A where id in(select id from B);
可以这么理解:in后面跟的是小表,exists后面跟的是大表
select * from A where id in (select id from B)也可以写成select id from B bleft joinselect id from A a where a.id = b.id