select case when aa=1 and bb=1 then '都为1' else '不都为1' end from tb
SELECT * FROM tb AS A WHERE NOT EXISTS(SELECT * FROM tb WHERE A.aa=aa AND bb<>1)
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*/
--如果存在记录,就说明bb有不等于1的 select * from ta where aa=1 and bb<>1
if exists(select * from ta where aa=1 and bb<>1) print 'bb不都为1'
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'
FROM tb AS A
WHERE NOT EXISTS(SELECT * FROM tb WHERE A.aa=aa AND bb<>1)
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*/
--如果存在记录,就说明bb有不等于1的
select * from ta where aa=1 and bb<>1
print 'bb不都为1'
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'