新手求助;
存在一个记录实体a和实体b的关系表(3 columns):‘id,a,b’. 如何查询至少具有b=x,b=y,b=z.......的a。
(除了自我join 的方法,由于关系表太大,多次连接数据库会挂) 

解决方案 »

  1.   

    select a
    from tb
    where b in ('x','y','z')
    group by a 
    having count(distinct b)=3
      

  2.   


    select 
    from 关系表 t
    where b=x
    and exists (select 1 from 关系表 where a=t.a and b=y)
    and exists (select 1 from 关系表 where a=t.a and b=z)
      

  3.   

    我也是这么想的但是没解决b重复问题,原来distinct 还可以用这个位置,学习了。
      

  4.   


    这个效率比起#2会更高吗?(假设会有很多Item,不止x,y,z)
    Thanks in advance