2013-04-18 74 views
2

我是PHP和MySQL的新手,並且正在爲此苦苦掙扎...我正在從PHP插入頁面收集表單數據。這是一份調查問卷。有20個問題,所有答案將是ABC。該表有一個id列和每個問題的列(Q1, Q2, Q3... Q20)。數據可能看起來像這樣;現在如何計算單行的字段值?

+------+-----+ 
| id | 1 | 
+------+-----+ 
| Q1 | A | 
| Q2 | B | 
| Q3 | A | 
| . | . | 
| . | . | 
| . | . | 
| Q20 | C | 
+------+-----+ 

,我所要做的是計算ABC多少值出現在單行(與id=1爲例)。

我發現了很多方法來計算多列中的值,但到目前爲止還沒有找到一種方法來統計/分組單個行中的值。

回答

0

這裏是你如何可以與一個MySQL查詢做到這一點:

select id, 
    sum(case when `1` = 'A' then 1 else 0 end) as CountA, 
    sum(case when `1` = 'B' then 1 else 0 end) as CountB, 
    sum(case when `1` = 'C' then 1 else 0 end) as CountC 
from SurveyTable 
group by id 
order by id; 

這裏有一個SQL Fiddle有限的測試數據。


附錄。卡洛斯發佈了一個更新的結構,導致以下答案。希望這些都是接近:)

這會給你一個很寬行總計:

select 
    sum(case when Q1 = 'A' then 1 else 0 end) as Q1CountA, 
    sum(case when Q1 = 'B' then 1 else 0 end) as Q1CountB, 
    sum(case when Q1 = 'C' then 1 else 0 end) as Q1CountC, 
    sum(case when Q2 = 'A' then 1 else 0 end) as Q2CountA, 
    sum(case when Q2 = 'B' then 1 else 0 end) as Q2CountB, 
    sum(case when Q2 = 'C' then 1 else 0 end) as Q2CountC, 
    sum(case when Q3 = 'A' then 1 else 0 end) as Q3CountA, 
    sum(case when Q3 = 'B' then 1 else 0 end) as Q3CountB, 
    sum(case when Q3 = 'C' then 1 else 0 end) as Q3CountC, 
    sum(case when Q4 = 'A' then 1 else 0 end) as Q4CountA, 
    sum(case when Q4 = 'B' then 1 else 0 end) as Q4CountB, 
    sum(case when Q4 = 'C' then 1 else 0 end) as Q4CountC, 
    sum(case when Q5 = 'A' then 1 else 0 end) as Q5CountA, 
    sum(case when Q5 = 'B' then 1 else 0 end) as Q5CountB, 
    sum(case when Q5 = 'C' then 1 else 0 end) as Q5CountC 
from SurveyTable; 

如果你想獲得每個問題一列,然後試試這個:

select 
    QuestionID, 
    sum(case when Answer = 'A' then 1 else 0 end) as CountA, 
    sum(case when Answer = 'B' then 1 else 0 end) as CountB, 
    sum(case when Answer = 'C' then 1 else 0 end) as CountC 
from (
    select 'Question1' as QuestionID, Q1 as Answer from surveytable 
    union all select 'Question2', Q2 from surveytable 
    union all select 'Question3', Q3 from surveytable 
    union all select 'Question4', Q4 from surveytable 
    union all select 'Question5', Q5 from surveytable) x 
group by QuestionID 

有小提琴here


另附錄:計數需要通過ID,因爲有一個ID每行有沒有必要爲SUM

這改變了方法。它首先將答案串在一起:

concat(q1,q2,q3,q4,q5) -- result for ID=1 in the test data: 'ABCAC' 

...然後它吸收從字符串的每A發生:

replace(concat(q1,q2,q3,q4,q5), 'A', '') -- result for ID=1: 'BCC' 

...第一串(ABCAC)的長度5,並且所述第二串(BCC)是長度3的長度差是數A答案:2。這就好像我可以解釋這一點。現在查詢:

select 
    id, 
    5 - length(replace(concat(q1,q2,q3,q4,q5), 'A', '')) AS CountA, 
    5 - length(replace(concat(q1,q2,q3,q4,q5), 'B', '')) AS CountB, 
    5 - length(replace(concat(q1,q2,q3,q4,q5), 'C', '')) AS CountC 
from surveytable; 

更新的小提琴是here

這隻給出原始數據。格式化會有點棘手,但它不應該太糟糕,特別是如果你用前端語言來做。如果必須使用MySQL對於這一點,它可能會更容易把上面爲子查詢和外部查詢應用的格式:

select 
    id, 
    CONCAT('You have chosen ' ...and miles of formatting logic using CountA, etc) 
from (
    select 
    id, 
    5 - length(replace(concat(q1,q2,q3,q4,q5), 'A', '')) AS CountA, 
    5 - length(replace(concat(q1,q2,q3,q4,q5), 'B', '')) AS CountB, 
    5 - length(replace(concat(q1,q2,q3,q4,q5), 'C', '')) AS CountC 
    from surveytable) x 
+0

謝謝埃德,這看起來很有趣,但我的表格結構實際上更像這樣;創建表surveytable(id int,'Q1' char(1),'Q2' char(1),'Q3' char(1),'Q4' char(1),'Q5' char(1)); (1,「A」,「B」,「C」,「A」,「C」), (2,'A','A','A','B' ,'C'), (3,'B','B','A','C','C'); – Carlos

+0

@卡洛斯 - 你的表格結構與你的評論截然分開。對不起 - 忽略 - 我現在看到它。我馬上更新我的答案。 –

+0

是的,對不起,我是新來的stackoverflow,還沒有格式化的懸念。 – Carlos

0

在PHP中,你可以在查詢的結果加載到arary,然後用array_count_values讓每個答案的計數:

$array = array(); 

while ($row = $result->fetch_assoc()) { 
    $array[] = $row; 
} 

// print_r(array_count_values($array); 

foreach(array_count_values($array) as $key => $value) 
{ 
    echo 'Answer ' . $key . ' was chosen ' . $value . ' times <br>'; 
} 

Working Fiddle

+0

嗨埃文,我會如何餡記錄的值(一個完整的行)到數組中? – Carlos

+0

@Carlos你使用'while循環'做了這個,如上所示。如果這沒有意義,我可以嘗試使它更清楚,但是你注意到了那部分? –

+0

啊,不,我沒有注意到,我正在看小提琴上的代碼......你可以爲我擴展它(我習慣於ASP,並且是PHP的新手!) – Carlos

0

免責聲明:從內存中,因此可以不確切的工作。

這個怎麼樣?

SELECT 1 AS 1, (SELECT COUNT(1)FROM問題WHERE 1 = 'A')AS A, (SELECT COUNT(1)FROM問題WHERE 1 = 'B')AS B, (SELECT COUNT(1)FROM WHERE 1 ='C')AS C

希望這會有所幫助。