sql查询简化,减轻负担

时间:2012-08-04 14:01:53

标签: sql

我需要通过使用连接或任何其他方式尽可能简化以下查询...

select polid
  from polver
  where idmkey = 'idm3'
    and polid in ( select polid
                     from polver
                     where idmkey = 'idm2'
                       and polid in ( select polid
                                        from polver
                                        where idmkey='idm1' ) );

2 个答案:

答案 0 :(得分:3)

您可以使用以下组:

select  polid
from    polver
group by
        polid
having  max(case when idmkey = 'idm1' then 1 end) = 1
        and max(case when idmkey = 'idm2' then 1 end) = 1
        and max(case when idmkey = 'idm3' then 1 end) = 1

答案 1 :(得分:3)

select polid from polver
where idmkey in ('idm1', 'idm2', 'idm3') 
group by polid
having count(distinct idmkey) = 3