有名为'mytable'的表,主键id,billNo事实上也不重复,customerId会重复,比如有4条记录如下:
id billNo customerId state1 state2 state3
1 0001 abbc 0 0 1
2 0002 csdf 0 0 0
3 0003 abbc 0 0 0
4 0004 ssss 0 0 0 希望查询客户名下所有bill的三个状态(state1、state2、state3)皆为0的客户。
查询结果共2个记录如下:
customerId
csdf
ssss
客户abbc因为0001的bill下有个state3为1所以不符合。
请教各位,不使用自定义函数,可以用一个sql(可以包含子查询,但尽量考虑效率)实现吗?

解决方案 »

  1.   

    select * from mytable a
    where not exists 
    (select 1 from mytable 
    where id=a.id and (state1=0 or state2=0 or state3=0))
      

  2.   

    忙中出错是不等于
    select * from mytable a
    where not exists 
    (select 1 from mytable 
    where id=a.id and (state1<>0 or state2<>0 or state3<>0))
      

  3.   

    select * from mytable a
    where exists 
    (select 1 from mytable 
    where id=a.id and (state1=0 or state2=0 or state3=0))
      

  4.   

    汗 如果都是非空的话,可以用
    select customerId  from mytable a 
    group by customerId 
    having max(state1)=0 and max(state2)=0 and max(state2)=0