Mysql 在一个具有不同字段的表中选择多行

Mysql 在一个具有不同字段的表中选择多行,mysql,Mysql,我有以下疑问: 从表_1中选择计数(id),其中字段_1=1 从表_1中选择计数(id),其中字段_2=1 从表_1中选择计数(id),其中字段_2=1 这可以在单个查询中完成吗。。 仅使用一个表,但有3个输出 比如: count(id)| count(id)| count(id) 12 | 44 | 55 是,您可以使用聚合函数获得结果,聚合函数的大小写表达式与以下类似: select sum(case when field_1 = 1 then 1 else

我有以下疑问:

从表_1中选择计数(id),其中字段_1=1

从表_1中选择计数(id),其中字段_2=1

从表_1中选择计数(id),其中字段_2=1

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

count(id)| count(id)| count(id)
12 | 44 | 55
是,您可以使用聚合函数获得结果,聚合函数的大小写表达式与以下类似:

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…
表达式

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
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