+ -
当前位置:首页 → 问答吧 → SQL 中使用in语句

SQL 中使用in语句

时间:2009-10-17

来源:互联网

在写sql时,听说过in语句的执行效率比较低,
但是下面的情况我必须要用in语句,
如一个表customer的主键为ID,
我在程序里面找到了要取出来的所有的customer的ID
然后我要根据这些ID把customer的详细信息从数据库中取出来,我只好用in了

不知道这种情况有没有不用in语句的办法

作者: bitengda   发布时间: 2009-10-17

exists

作者: fredrickhu   发布时间: 2009-10-17

其实效率都差不多

作者: fredrickhu   发布时间: 2009-10-17

差不多

作者: xiequan2   发布时间: 2009-10-17

引用 1 楼 fredrickhu 的回复:
exists

能用到索引

作者: SQL77   发布时间: 2009-10-17

学习了..........

作者: LCAAA   发布时间: 2009-10-17

引用 4 楼 sql77 的回复:
引用 1 楼 fredrickhu 的回复:
exists

能用到索引

作者: mingjunwang   发布时间: 2009-10-17

引用楼主 bitengda 的回复:
在写sql时,听说过in语句的执行效率比较低,
但是下面的情况我必须要用in语句,
如一个表customer的主键为ID,
我在程序里面找到了要取出来的所有的customer的ID
然后我要根据这些ID把customer的详细信息从数据库中取出来,我只好用in了

不知道这种情况有没有不用in语句的办法

select m.* ,n.*
from customer m, (...其他表,或子查询) n
where m.id = n.id

作者: dawugui   发布时间: 2009-10-17

作者: devilidea   发布时间: 2009-10-17

楼上说的对的

作者: lannikyd   发布时间: 2011-11-19

索引

作者: xiaolinyouni   发布时间: 2011-11-19

把你的表结构、数据、和想要的结果贴出来

作者: pengxuan   发布时间: 2011-11-19

SQL code
select * from 大表 where cc in (小表)
select * from 小表 where exists(大表)

表A(小表),表B(大表)

1select * from A where cc in (select cc from B) 
       效率低,用到了A表上cc列的索引;
   select * from A where exists(select cc from B where cc=A.cc) 
       效率高,用到了B表上cc列的索引。

select * from table where id in('1','2')

select * from table t where exists(select id from table where id=t.id) 

作者: hllfl   发布时间: 2011-11-19

热门下载

更多