select distinct aa
from tablename t1
where bb='1'
and exists (
select * from tablename t2
where t2.aa=t1.aa
and t2.bb='2'
)
and exists (
select * from tablename t3
where t3.aa=t1.aa
and t3.bb='3'
)这不是什么难事,问题是你的例子错误,结果应该只有test1 。

解决方案 »

  1.   

    select distinct aa
       from ... 
      where  bb= '1'
      

  2.   

    同意haiwer(海阔天空--回复语句只对MSSQL有效)
      

  3.   

    bb同时等于1,2,3的有test1,test3,条件可以任意组合,难道没有人会吗?我觉的表结构有问题,但不知如何建?
      

  4.   

    Haiwer(海阔天空--回复语句只对MSSQL有效)的语句可以。
    这样会不会清楚一点:
    select distinct aa from tb as bbb
    where (exists (select * from tb as aaa where aaa.bb='1' and aaa.aa=bbb.aa)
    and
    exists (select * from tb as aaa where aaa.bb='2' and aaa.aa=bbb.aa)
    and
    exists (select * from tb as aaa where aaa.bb='3' and aaa.aa=bbb.aa)
    )
    你说的条件可以任意是什么意思?
      

  5.   

    select distinct aa 
    from table1 as
    group by aa
    having bb='1' and bb='2' and bb='3' and (aa <>'test1' and aa <>'test3') 
    union all
    select distinct aa 
    from table1 as
    group by aa
    having bb='1' or bb='2' or bb='3' and (aa in('test1','test3')) 
      

  6.   

    上面一个有问题,这个应该是对的,大家看看!select distinct aa 
    from table1 as
    group by aa
    having bb='1' and bb='2' and bb='3' 
    Where aa <>'test1' and aa <>'test3'
    union all
    select distinct aa 
    from table1 as
    group by aa
    having bb='1' or bb='2' or bb='3' 
    Where aa in('test1','test3')
      

  7.   

    bb同时等于1,2,3的有test1,test3,条件可以任意组合,难道没有人会吗?我觉的表结构有问题,但不知如何建?select aa 
    from (
    select distinct aa,bb from table1 
    ) as c
    where bb='1' or bb='2' or bb='3'
    group by aa
    having count(*>1) 
      

  8.   

    复杂吗?
    select aa from tablename group by aa
    having sum((case bb when 1 then 1 when 2 then 1 when 3 then 1 else 0 end))=3
      

  9.   

    我认为:
    表结构修改如下较好:
      字段 名    类型
       aa        varchar  可重复
       bb1       varchar   '0' 或 '1'
       bb2       varchar   '0' 或 '1'
       bb3       varchar   '0' 或 '1'
      

  10.   

    select aa from tablename group by aa
    having sum((case bb when 1 then 1 when 2 then 1 when 3 then 1 else 0 end))>1 
      

  11.   

    如果原表有重复,dut(大工) 的正确,但有个笔误:
    select aa 
    from (
    select distinct aa,bb from table1 
    ) as c
    where bb='1' or bb='2' or bb='3'
    group by aa
    having count(*) >1
      

  12.   

    这个问题并不复杂,但是贴主原说:
    “bb字段值=1 and 2 and 3 的记录的aa字段的值(不能重复),”
    后说:
    “bb同时等于1,2,3的有test1,test3,条件可以任意组合”
    还是不对,怎么会有test1,test3呢,怎么叫“条件可以任意组合”
    看看你自己的数据,有test1,test3又怎么没有test2呢???浪费感情!!上面两个回复都是没有用的,根本不知道要什么!!