2015-11-06 48 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

相關問題