select column1,column2,column3 from table1 where column5=0 and column6=0
select column1,column2,column3 from table1 where column5!=0 and column6!=0
这两个sql语句从同一个table1读取数据。有没有办法编写一个返回相同数据的查询?
我想在单个查询中获得(column5 = 0 AND column6 = 0)和(column5!= 0 AND column6!= 0)的单独结果。
as example:
select column1 as c1,column2 as c2,column3 as c3 from table1 where column5=0 and column6=0
union
select column1 as c1_,column2 as c2_,column3 as c3_ from table1 where column5!=0 and column6!=0
答案 0 :(得分:1)
SELECT column1, column2, column3
FROM table1
WHERE (column5 = 0 AND column6 = 0) OR (column5 != 0 AND column6 != 0)
答案 1 :(得分:0)
如果您希望从单个结果集中的两个查询中获取所有结果,则可以像这样使用UNION:
select column1,column2,column3 from table1 where column5=0 and column6=0
union
select column1,column2,column3 from table1 where column5!=0 and column6!=0
答案 2 :(得分:0)
如其他人所提到的,您可以使用UNION。为了识别哪些记录来自哪个查询,你可以这样做:
select 'query1', column1,column2,column3 from table1 where column5=0 and column6=0
union
select 'query2', column1,column2,column3 from table1 where column5!=0 and column6!=0
然后在记录集处理中,您可以检查第一个值并决定如何处理其余值。
答案 3 :(得分:0)
SELECT column1,column2,column3 from table1 where (column5=0 and column6=0) or (column5!=0 and column6!=0)
答案 4 :(得分:0)
如果我理解正确,你想要将结果的两个部分分开。
为此添加ORDER BY
子句。
SELECT column1, column2, column3
FROM table1
WHERE (column5 = 0 AND column6 = 0)
OR (column5 != 0 AND column6 != 0)
ORDER BY (column5 = 0) DESC -- it's enough to include one column in this case.
如果您还希望能够区分它们,请添加另一个表示原点的列:
SELECT (column5 = 0) AS first_part, column1, column2, column3
FROM table1
WHERE (column5 = 0 AND column6 = 0)
OR (column5 != 0 AND column6 != 0)
ORDER BY 1 DESC;
答案 5 :(得分:0)
select
sum(
case when (column5=0 or column6=0 )
then 1 else 0 end
) as c1 ,
sum(
case when (column5=0 or column6=0 )
then 1 else 0 end
) as c2 ,
sum(
case when (column5=0 or column6=0 )
then 1 else 0 end
) as c3 ,
sum(
case when (column5!=0 or column6!=0 )
then 1 else 0 end
) as c1_ ,
sum(
case when (column5!=0 or column6!=0 )
then 1 else 0 end
) as c2_ ,
sum(
case when (column5!=0 or column6!=0)
then 1 else 0 end
) as c3_ ,
from table1