2017-04-20 41 views
0

我有一個關於具有ID的兩個相關列的問題和答案的表格。對這些問題的回答可以是數字或字母數字這樣的:使用`group_concat`和`group by`旋轉表格

╔══════════╦═════════════╦═══════════╦═════════╗ 
║ visit_id ║ visit_child ║ question ║ answer ║ 
╠══════════╬═════════════╬═══════════╬═════════╣ 
║ 121340 ║  104280 ║ How much? ║ 3  ║ 
║ 121340 ║  104280 ║ How many? ║ 2  ║ 
║ 121340 ║  104280 ║ Why?  ║ Because ║ 
║ 121340 ║  104280 ║ Really? ║ Yup  ║ 
║ 121485 ║  114190 ║ How much? ║ 5  ║ 
║ 121485 ║  114190 ║ How many? ║ 6  ║ 
║ 121485 ║  114190 ║ Why?  ║ Why not ║ 
║ 121485 ║  114190 ║ Really? ║ Sure ║ 
╚══════════╩═════════════╩═══════════╩═════════╝ 

我想這樣的問題,成爲列轉動此信息,並給出答案得到妥善安置。我正在尋找類似下面這樣的結果:

╔══════════╦═════════════╦═══════════╦═══════════╦═════════╦═════════╗ 
║ visit_id ║ visit_child ║ How much? ║ How many? ║ Why? ║ Really? ║ 
╠══════════╬═════════════╬═══════════╬═══════════╬═════════╬═════════╣ 
║ 121340 ║  104280 ║   3 ║   2 ║ Because ║ Yup  ║ 
║ 121485 ║  114190 ║   5 ║   6 ║ Why not ║ Sure ║ 
╚══════════╩═════════════╩═══════════╩═══════════╩═════════╩═════════╝ 

我做我的功課,在MySQL教程讀了幾HOWTO支點和我來到這個:

SELECT 
    infoBase.visit_id, 
    infoBase.visit_child, 
    GROUP_CONCAT(DISTINCT 
     CONCAT('MAX(IF(infoBase.question = \'',question,'\', \'',answer,'\', 0)) AS \'',question,'\'',''), "\n" 
    ) 
FROM  
    visits vi, 
    infoBase 
WHERE 
    vi.id = infoBase.visit_child 
GROUP BY 
    infoBase.visit_id, infoBase.visit_child 

我能得到什麼如下:

╔══════════╦═════════════╦════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗ 
║ visit_id ║ visit_child ║                GROUP_CONCAT(DISTINCT CONCAT('MAX(IF(infoBase.question = \'',question,'\', \'',answer,'\', 0)) AS \'',question,'\'',''), "\n")                ║ 
╠══════════╬═════════════╬════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╣ 
║ 121340 ║  104280 ║ MAX(IF(infoBase.question = 'How much?', '3', 0)) AS 'How much?',MAX(IF(infoBase.question = 'How many?', '2', 0)) AS 'How many?',MAX(IF(infoBase.question = 'Why?', 'Because', 0)) AS 'Why?',MAX(IF(infoBase.question = 'Really?', 'Yup', 0)) AS 'Really?' ║ 
║ 121485 ║  114190 ║ MAX(IF(infoBase.question = 'How much?', '5', 0)) AS 'How much?',MAX(IF(infoBase.question = 'How many?', '6', 0)) AS 'How many?',MAX(IF(infoBase.question = 'Why?', 'Why not', 0)) AS 'Why?',MAX(IF(infoBase.question = 'Really?', 'Sure', 0)) AS 'Really?' ║ 
╚══════════╩═════════════╩════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝ 

通過group_concat檢索的結果是正確的,但我想,讓我們說「轉換」該字符串real列。 這必須儘可能動態地完成,因爲在真正的表格中,我不知道問題的確切數量和它們是什麼,所以我不能在許多group_concat中硬編碼問題。 我在做什麼錯?

謝謝!

P.S.這在一個更大的查詢過度簡化。如果在出現新問題時需要添加更多信息,我會添加它。

+0

[用動態列的MySQL透視表查詢]的可能的複製(http://stackoverflow.com/questions/12598120/mysql-pivot-table-query-with-dynamic-columns) – shmosel

+0

@shmosel似乎相似是的,我以前沒有看到過這個問題。我會盡力理解那裏的答案。謝謝。 – Metafaniel

+0

[_Here's_](http://mysql.rjweb.org/doc.php/pivot)如何動態生成查詢。把它存入一個存儲過程,然後準備並執行它。 「調用」這個過程將在「一步」中完成。 –

回答

1

嘗試像這樣的查詢。它沒有測試!

SELECT 
    infoBase.visit_id, 
    infoBase.visit_child, 
    GROUP_CONCAT(IF(question = 'How much?', answer ,'') SEPARATOR '') AS 'How much?' , 
    GROUP_CONCAT(IF(question = 'How many?', answer ,'') SEPARATOR '') AS 'How many?' , 
    GROUP_CONCAT(IF(question = 'Why?',  answer ,'') SEPARATOR '') AS 'Why?' , 
    GROUP_CONCAT(IF(question = 'Really?', answer ,'') SEPARATOR '') AS 'Really?' 
    ) 
FROM  
    visits vi, 
    infoBase 
WHERE 
    vi.id = infoBase.visit_child 
GROUP BY 
    infoBase.visit_id; 
+0

好吧,我明白了,但我試圖儘可能動態地做到這一點,因爲在真正的表格中,我不知道有多少個問題,它們是什麼。所以我不能硬編碼的問題。我相應地更新了我的問題。謝謝 – Metafaniel

+0

我看到你使用了許多'group_concat',每列一個。有沒有辦法模擬這動態?使用循環或什麼? – Metafaniel