表table如下
A     B
11    1
11    2
12    1
12    1
13    1
14    3
14    3
15    1
15    3
现在要取出的结果如下; B为1,A所对应的B里只能为1,不能为其它,
结果如下:
12    1
12    1
13    1
请问这段SQL怎么写?? 

解决方案 »

  1.   

    Select * from table t Where B=1 and not exists
     (Select 1 from table where A=t.A and B<>1)
      

  2.   

    表table如下 
    A     B 
    11    1 
    11    2 
    12    1 
    12    1 
    13    1 
    14    3 
    14    3 
    14    1   -->
    14    1   -->
    15    1 
    15    3 
    现在要取出的结果如下; B为1,A所对应的B里只能为1,不能为其它, 
    结果如下: 
    12    1 
    12    1 
    13    1 
    请问这段SQL怎么写?? 
      

  3.   

    select zz.a,
           zz.max_num
      from (
            select tt.a,
                   max(tt.b) as max_num,
                   min(tt.b) as min_num
              from tablename tt
             group by tt.a
            )zz
     where zz.max_num = 1
       and zz.min_num = 1;