有一个表T
     C1      C2
     -----------
     a        1
     a        2
     b        1
     b        2
     b        3
     c        2
我想要查询出来c1,所对应的行中 有 c2=2 也有  C2=3 可是不知道怎么写SQL语句,求教。

解决方案 »

  1.   

    select c1,max(c2) as c2
    from t
    group by c1是不是这个?
      

  2.   

    select 
        a.* 
    from 
        T a 
    where 
        exists(select 1 from T where C1=a.C1 and C2=2)
        and
        exists(select 1 from T where C1=a.C1 and C2=3)
      

  3.   

    楼上理解select 
        distinct c1
    from 
        T a 
    where 
        C2=2
        and
        exists(select 1 from T where C1=a.C1 and C2=3)
      

  4.   

    樓主是指這個?
    create table t (c1 varchar(02),c2 int)
    insert into t
    select 'a',1
    union
    select 'a',2
    union
    select 'b',1
    union
    select 'b',2
    union
    select 'b',3
    union
    select 'c',2select c1 from T 
    where c1 in (select c1 from T where c2=2) 
    and c2=3
    order by c1drop table t/*The result:*/c1
    ----
    b