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
posted @ 2022-03-03 10:12  一刹流云散  阅读(93)  评论(0编辑  收藏  举报