0

我有以下查询:

SELECT count(id) from table_1 WHERE field_1 = 1

SELECT count(id) from table_1 WHERE field_2 = 1

SELECT count(id) from table_1 WHERE field_2 = 1

这可以在单个查询中完成吗?只使用一个表,但有 3 个输出,例如:

    count(id) | count(id) | count(id)<br>
    12        | 44        | 55
4

2 回答 2

2

是的,您可以通过使用具有类似于以下 CASE 表达式的聚合函数来获取结果:

select
  sum(case when field_1 = 1 then 1 else 0 end) field1Total,
  sum(case when field_2 = 1 then 1 else 0 end) field2Total
from table_1

sum(case...)您将为要总计的剩余项目添加更多表达式。

于 2013-08-23T16:50:27.743 回答
0
Select Distinct (Select Count(id) from table_1 where field1=1)as id1, (Select Count(id) from table_1 where field2=1)as id2  from table_1
于 2013-08-23T17:02:27.727 回答