sql中的in转为exists语句

今天看项目代码里面有许多in的sql语句,效率比较低,而且in里面的内容比较多的时候也会引发异常。

这里记一下in转为exists的方法:

原始sql语句:

select * from t where id in (1,2,3,4,5)

 转化成exists后,

  

select * from twhere id exists (select * from (select 1 as x union select 2 union select 3 union select 4 union select 5)tmp where tmp.id = t.id)

猜你喜欢

转载自iqbon.iteye.com/blog/1931779