有表Association如下:
aid bid
 1   1
 2   1
 3   2
 4   2
 5   3
 ......
求一SQL语句,得到所有与aid(1,2)同时存在关联的bid

解决方案 »

  1.   

    select bid
    from Association a
    where a.aid = 1
    and exists(
        select 1 from Association b
        where b.aid = 2 and a.bid = b.bid
    )
      

  2.   

    (1, 2)只是个例子,这个序列是不固定的,可以描述为(e0, e1, e2... en)
      

  3.   

    select a.bid from Association a,Association b
    where a.bid=b.bid and a.aid=1 and b.aid=2;
      

  4.   

    select bid from
    (
    select distinct bid from tb where aid = 1
    union all
    select distinct bid from tb where aid = 2
    ) t
    group by bid having count(1) > 1
      

  5.   

    就是存在
    aid bid
      1   x
      2   x
    这样的记录
      

  6.   

    select bid from association
    where aid in (e0, e1, e2... en)
    group by bid
    having count(distinct aid)=N
      

  7.   

    select bid from association 
    where aid in (e0, e1, e2... en) 
    group by bid 
    having count(distinct aid)=N