select * from book b where bookid in (select id from class);
select * from book b where exists (select 1 from class where b.bookid=id );
select * from book b where bookid in (select id from class); 它的运行方式是这样的,类似于俩个for循环 for select * from class for select * from book where book.id=class.id select * from book b where exists (select 1 from class where b.bookid=id ); exists的运行方式如下 for select * from book for select * from class where class.id=book.id
以上俩个案例简单来说 :
- 如果查询的两个表大小相当,那么用in和exists差别不大。
- 如果两个表中一个较小,一个是大表,则子查询表大的用exists,子查询表小的用in: