我有以下疑问:
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
答案 0 :(得分: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...)
表达式。
答案 1 :(得分: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