有名为'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(可以包含子查询,但尽量考虑效率)实现吗?
[code="sql"]select distinct customerId from mytable
where customerId not in
(select distinct customerId from mytable
where (state1|state2|state3)=1)
[/code]
太简单了
[code="sql"]
select distinct customerid
from mytable
where state1 =0
and state2 =0
and state3 = 0
[/code]
试试这个:
[code="sql"]select distinct customerId from mytable
where customerId not in (select distinct customerId from mytable where (state1|state2|state3)=1)[/code]