2017-02-07 106 views
2

這是我的第一個問題,所以我希望我不要搞砸了。Mysql查詢與條件的另一列

這裏有一個SQL小提琴我提出幫我解釋一下:http://sqlfiddle.com/#!9/7f2b5c/7

我們有問題,答案question_types和answer_types。

我想知道每個問題,有多少人已經回答了每個answer_type以及有多少人回答了這個問題,但是我無法獲得總數。

這就是我得到:

id | fk_question_type | description   | num | value  | total 
1 | 1    | How was the breakfast? | 0 | Bad  | 0 
1 | 1    | How was the breakfast? | 1 | Good  | 1 
1 | 1    | How was the breakfast? | 0 | Indifferent| 0 
1 | 1    | How was the breakfast? | 2 | Very good | 2 
2 | 1    | How was the lunch?  | 0 | Bad  | 0 
2 | 1    | How was the lunch?  | 1 | Good  | 1 
2 | 1    | How was the lunch?  | 0 | Indifferent| 1 
2 | 1    | How was the lunch?  | 1 | Very good | 1 

這是我想什麼:

id | fk_question_type | description   | num | value  | total 
1 | 1    | How was the breakfast? | 0 | Bad  | 3 
1 | 1    | How was the breakfast? | 1 | Good  | 3 
1 | 1    | How was the breakfast? | 0 | Indifferent| 3 
1 | 1    | How was the breakfast? | 2 | Very good | 3 
2 | 1    | How was the lunch?  | 0 | Bad  | 2 
2 | 1    | How was the lunch?  | 1 | Good  | 2 
2 | 1    | How was the lunch?  | 0 | Indifferent| 2 
2 | 1    | How was the lunch?  | 1 | Very good | 2 

查詢我想現在:

SELECT id, fk_question_type, description, num, value, SUM(num) AS totalAnswers 
FROM (
    (SELECT q.id, q.fk_question_type, q.description, COUNT(a.id) AS num, at.value 
    FROM answer a 
    LEFT JOIN question q ON a.`fk_question`=q.`id` 
    LEFT JOIN answer_type at ON at.id = a.`fk_answer_type` 
    GROUP BY q.id, at.id ORDER BY q.id, at.id) 
    UNION ALL 
    (SELECT q.id, q.fk_question_type, q.description, 0 AS num, at.value 
    FROM answer a 
    LEFT JOIN question q ON a.`fk_question`=q.`id` 
    LEFT JOIN answer_type at ON at.fk_question_type=q.fk_question_type 
    GROUP BY q.id, at.id ORDER BY q.id, at.id) 
) AS T 
WHERE fk_question_type = 1 
GROUP BY id, value ORDER BY id 

如何在保留其他列的同時彙總具有相同ID的行的數量?

+1

你真的需要使用'LEFT JOIN'代替'INNER JOIN'?你真的能有一個沒有相應問題的答案嗎? – Barmar

+0

從小提琴的答案中,可以看到問題「早餐怎麼樣?」,「午餐怎麼樣?」和「你會再來?」每個問題三次。 「晚餐怎麼樣」只問了兩次。有人指出,你能否詳細說明你想在'num'中看到什麼以及'total'中有什麼? –

+0

@DhruvSaxena我想'num'已經是它的樣子了,'早餐是怎麼回事','Bad'值是0次,1次是'Good',0次是'無所謂',2次是'非常好'。 '總數'應該是問題回答的次數,所以這是每行'早餐怎麼樣'的3倍?' 對不起,如果我沒有說清楚。 – EnricSV

回答

0

使用子查詢獲取每個id的總和,並將其與通過獲取每個值行的查詢進行連接。

而且在外部查詢,你應該使用SUM(num)MAX(num)num值從UNION合併這兩個查詢。

SELECT T.id, fk_question_type, description, SUM(num) AS num, value, T2.totalAnswers 
FROM (
    (SELECT q.id, q.fk_question_type, q.description, COUNT(a.id) AS num, at.value 
    FROM answer a 
    LEFT JOIN question q ON a.`fk_question`=q.`id` 
    LEFT JOIN answer_type at ON at.id = a.`fk_answer_type` 
    GROUP BY q.id, at.id ORDER BY q.id, at.id) 
    UNION ALL 
    (SELECT q.id, q.fk_question_type, q.description, 0 AS num, at.value 
    FROM answer a 
    LEFT JOIN question q ON a.`fk_question`=q.`id` 
    LEFT JOIN answer_type at ON at.fk_question_type=q.fk_question_type 
    GROUP BY q.id, at.id ORDER BY q.id, at.id) 
) AS T 
JOIN (SELECT q.id, COUNT(*) AS totalAnswers 
     FROM answer a 
     LEFT JOIN question q ON a.`fk_question`=q.`id` 
     LEFT JOIN answer_type at ON at.id = a.`fk_answer_type` 
     GROUP BY q.id) AS t2 ON T.id = T2.id 
WHERE fk_question_type = 1 
GROUP BY T.id, value 

DEMO

+0

謝謝!我接受了你的答案。 – EnricSV