我现在有这样的一个sql语句
select mbrid from fi where cpt in('77425','77427')
怎么才能改成用exists啊
谢谢

解决方案 »

  1.   

    select mbrid from fi
    where exists(
      select 1 from(
        select '77425' a from dual uinon all 
        select '77427' from dual)
      where a=cpt)
    但就这个语句来说,exists完全没必要。

    select mbrid from fi where cpt in(select id from a)
    in后面跟的是大数据集的时候,可以用exists来代替,避免全表遍历:
    select mbrid from fi where exists(select 1 from a where id=fi.cpt)数据集小的时候,用in
    并非所有情况下exists都优于in
      

  2.   


    select mbrid from fi where cpt in('77425','77427');这句sql本来已经很简洁了,在这种情况下in比exists效率要高的,楼主为什么要改成exists呢?改成如下的exists语句即可:
    select mbrid from fi 
    where exists
       (
         select 1 from 
         (select '77425' as nc from dual union select '77427' from dual)a 
         where fi.cpt =a.nc
       )