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 1=1) 或 (select * from 数据表2 where y=t.x)
例子:
in的SQL语句
SELECT id, category_id, htmlfile, title, convert(varchar(20),begintime,112) as pubtime
FROM tab_oa_pub WHERE is_check=1 and
category_id in (select id from tab_oa_pub_cate where no='1')
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_check=1 and
exists (select id from tab_oa_pub_cate where tab_oa_pub.category_id=convert(int,no) and no='1')
order by begintime desc
效率比较:
先讨论IN和EXISTS。来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/223555/viewspace-717946/ ,如需转载,请注明出处,否则将追究法律责任。