如何从多个行和列中获取数据总数?

时间:2015-11-06 10:46:44

标签: mysql sql

我想得到整个表中的好的数量以及其他条目的各条目的数量。请帮帮我。

enter image description here

1 个答案:

答案 0 :(得分:1)

使用select SUM(case when op1 = 'good' then 1 else 0 end) + SUM(case when op2 = 'good' then 1 else 0 end) + SUM(case when op3 = 'good' then 1 else 0 end) + SUM(case when op4 = 'good' then 1 else 0 end) + SUM(case when op5 = 'good' then 1 else 0 end) + SUM(case when op6 = 'good' then 1 else 0 end) from tablename 返回1表示正常,0表示其他值。对每列求和,将这些总和加在一起。

select op, count(*)
from
(
select op1 as op from tablename
union all
select op2 from tablename
union all
select op3 from tablename
union all
select op4 from tablename
union all
select op5 from tablename
union all
select op6 from tablename
) as t
group by op

可替换地:

{{1}}