+ -
当前位置:首页 → 问答吧 → 求sql语句

求sql语句

时间:2011-11-24

来源:互联网

有a,b,c 三列
要筛选的结果要求为
1. a,b,c三列不能都为零 即不能出现(0,0,0)
2. a,b,c三列任一一列或两列为零时的数据条目
3. a,b,c三列都不为零,但如果其中任意一列小于10时,选出此条数据
要求符合以上条件
表名 table

作者: csdweilu   发布时间: 2011-11-24

第2个条件和第3个条件冲突

作者: pengxuan   发布时间: 2011-11-24

SQL code

select *
from tb
where a+b+c <> 0 
   and 
    ((a*b=0 or b*c=0 or a*c=0)
      or (a*b*c<>0 and (a<10 or b<10 or c<10)))

作者: AcHerat   发布时间: 2011-11-24

select * from table where a!=0 or b!=0 or c!=0

作者: fuxiyang   发布时间: 2011-11-24

SQL code
select * 
from tb
where not (a=0 and b=0 and c=0)
and ((a=0 and b=0 or a=0 and c=0 or b=0 and c=0)
or (a<>0 and b<>0 and c<>0 and (a<10 or b<10 or c<10)))

作者: qianjin036a   发布时间: 2011-11-24

SQL code
select * 
from tb
where not (a=0 and b=0 and c=0)
and ((a=0 and b=0 or a=0 and c=0 or b=0 and c=0)
or (a*b*c<>0 and (a<10 or b<10 or c<10)))

作者: qianjin036a   发布时间: 2011-11-24

更正:
SQL code
select * 
from tb
where not (a=0 and b=0 and c=0)
and ((a=0 or b=0 or c=0)
or (a*b*c<>0 and (a<10 or b<10 or c<10)))

作者: qianjin036a   发布时间: 2011-11-24

都不对,我自己想出来了,其实是考虑的太复杂了
SQL code

select * from  temptable where (a<10 or b<10 or c<10) and  not  (a=0 and b=0 and  c=0)

作者: csdweilu   发布时间: 2011-11-24

你这个结果和你前面的说法不符啊,你这样查询的话,如果三列都有值,但是三列都大于10,就不显示了。

作者: fuxiyang   发布时间: 2011-11-24

引用 7 楼 csdweilu 的回复:
都不对,我自己想出来了,其实是考虑的太复杂了

SQL code

select * from temptable where (a<10 or b<10 or c<10) and not (a=0 and b=0 and c=0)

你是要查询至少有一列不为0,并且至少有一列小于10的结果吧?

作者: fuxiyang   发布时间: 2011-11-24