2015-11-06 59 views
-4

我想要得到整个表和其他条目中的好的数目等每个条目的数目。请帮帮我。如何从多行和多列中获取总数据?

enter image description here

+0

能否请你给我一个例子,请您及时......我没有在SQL很多知识 – Anna

+1

您应该将表结构改变(ITEM_ID,op_id,得分),那么SELECT语句变得非常简单。 – Naktibalda

回答

1

使用CASE为其他值返回1为好,0。为每列添加总和,并将这些总和相加。

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 

或者:

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

非常感谢您的时间和精力。请告诉我如何在php中显示变量。获取资源#41 – Anna

+0

抱歉,无法帮助您。 (我不知道PHP。) – jarlh

+0

没问题。非常感谢你 – Anna

相关问题