表A 中有两列aa,bb  数据如下
 aa    bb
 1      1
 1      2
 1      1怎么才能判断出 当aa=1,bb是否都为1

解决方案 »

  1.   

    select * from ta where aa=1 and bb=1
      

  2.   

    select case  when aa=1 and bb=1  then '都为1' else '不都为1' end from tb
      

  3.   

    SELECT * 
    FROM tb AS A
    WHERE NOT EXISTS(SELECT * FROM tb WHERE A.aa=aa AND bb<>1)
      

  4.   

    declare @t table(aa int,bb int) 
    insert @t select 1   ,   1 
    insert @t select 1  ,    2 
    insert @t select 1 ,     1
    insert @t select 2,2
    select * from @t t where not exists(select 1 from @t where aa=t.aa and bb<>t.bb) 
    /*aa          bb
    ----------- -----------
    2           2*/
      

  5.   


    --如果存在记录,就说明bb有不等于1的
    select  * from ta where aa=1 and bb<>1
      

  6.   

    if exists(select  * from ta where aa=1 and bb<>1)
    print 'bb不都为1'
      

  7.   

    declare @tb table (aa int,bb int)
    insert into  @tb select 1,1
          union all  select 1,2
          union all  select 1,1
    if exists( select 1 from (
    select *,值=case when bb=1 THEN '1' else 0 end  from @tb where aa=1) a where 值=0)
    print '当aa=1,bb不是都是1'
    else 
    print '当aa=1,bb都是1'