彩网站开发,购物小程序源码,网页游戏网站排名,怎样做网络销售网站in 与 exist 的语法比较#xff1a; select from 数据表 t where t.x in (...) 括号内可以是符合t.x字段类型的值集合#xff0c;如(1,2,3)#xff0c;但如果t.x是number类型的时候#xff0c;似乎这样的写法会出问题#xff1b;也可以是通 过另外的sele…in 与 exist 的语法比较 select × from 数据表 t where t.x in (...) 括号内可以是符合t.x字段类型的值集合如(1,2,3)但如果t.x是number类型的时候似乎这样的写法会出问题也可以是通 过另外的select语句查询出来的值集合如(select y from 数据表2 where ...)。 select * from 数据表 t where [...] and exist (...) 方括号内为其它的查询条件可以没有。exist后面的括号内可以是任意的条件这个条件可以与外面的查询没有任何关系也可以与外面的条件结合。如 (select * from 数据表2 where 11) 或 (select * from 数据表2 where yt.x)例子 in的SQL语句
SELECT id, category_id, htmlfile, title, convert(varchar(20),begintime,112) as pubtime
FROM tab_oa_pub WHERE is_check1 and
category_id in (select id from tab_oa_pub_cate where no1)
order by begintime desc
exists的SQL语句
SELECT id, category_id, htmlfile, title, convert(varchar(20),begintime,112) as pubtime
FROM tab_oa_pub WHERE is_check1 and
exists (select id from tab_oa_pub_cate where tab_oa_pub.category_idconvert(int,no) and no1)
order by begintime desc效率比较 先讨论IN和EXISTS。 select * from t1 where x in ( select y from t2 ) 事实上可以理解为 select * from t1, ( select distinct y from t2 ) t2 where t1.x t2.y 如果你有一定的SQL优化经验从这句很自然的可以想到t2绝对不能是个大表因为需要对t2进行全表的“唯一排序”如果t2很大这个排序的性能是不 可忍受的。但是t1可以很大为什么呢最通俗的理解就是因为t1.xt2.y可以走索引。但这并不是一个很好的解释。试想如果t1.x和t2.y都 有索引我们知道索引是种有序的结构因此t1和t2之间最佳的方案是走merge join。另外如果t2.y上有索引对t2的排序性能也有很大提高。 select * from t1 where exists ( select null from t2 where y x ) 可以理解为 for x in ( select * from t1 ) loop if ( exists ( select null from t2 where y x.x ) then OUTPUT THE RECORD! end if end loop 这个更容易理解t1永远是个表扫描因此t1绝对不能是个大表而t2可以很大因为yx.x可以走t2.y的索引。 综合以上对IN/EXISTS的讨论我们可以得出一个基本通用的结论IN适合于外表大而内表小的情况EXISTS适合于外表小而内表大的情况。转载于:https://www.cnblogs.com/alicesunBlog/archive/2013/01/15/2861209.html