2013-03-27 3 views
3

J'ai du mal à trouver une solution à ce problème MySQL. Je n'arrive pas à comprendre comment le faire. J'ai les tableaux suivants.MySQL Pivot Table Colonne Données en tant que lignes

Question table 
+----+-------------+ 
| id | question | 
+----+-------------+ 
| 1 | Is it this? | 
| 2 | Or this? | 
| 3 | Or that? | 
+----+-------------+ 

Results Table 
+----+---------+--------+ 
| id | user_id | job_id | 
+----+---------+--------+ 
| 1 | 1  | 1  | 
| 2 | 1  | 3  | 
| 3 | 2  | 3  | 
+----+---------+--------+ 

Answers table 
+----+-------------------------+--------------+ 
| id | answer | fk_question_id | fk_result_id | 
+----+-------------------------+--------------+ 
| 1 | Yes | 1    | 1   | 
| 2 | No  | 2    | 1   | 
| 3 | Maybe | 3    | 1   | 
| 4 | Maybe | 1    | 2   | 
| 5 | No  | 2    | 2   | 
| 6 | Maybe | 3    | 2   | 
| 7 | Yes | 1    | 3   | 
| 8 | Yes | 2    | 3   | 
| 9 | No  | 3    | 3   | 
+----+-------------------------+--------------+ 

Si possible, je voudrais afficher les réponses aux questions que des colonnes pour chaque jeu de résultats, comme celui-ci.

+-----------+---------+--------+-------------+----------+----------+ 
| result_id | user_id | job_id | Is it this? | Or this? | Or that? | 
+-----------+---------+--------+-------------+----------+----------+ 
| 1   | 1  | 1  | Yes   | No  | Maybe | 
| 2   | 1  | 3  | Maybe  | No  | Maybe | 
| 3   | 2  | 3  | Yes   | Yes  | No  | 
+-----------+---------+--------+-------------+----------+----------+ 

Toute aide serait grandement appréciée.

Merci

Répondre

3
SELECT a.ID, 
     a.user_ID, 
     a.job_id, 
     MAX(CASE WHEN c.question = 'Is it this?' THEN b.answer END) 'Is it this?', 
     MAX(CASE WHEN c.question = 'Or this?' THEN b.answer END) 'Or this?', 
     MAX(CASE WHEN c.question = 'Or that? ' THEN b.answer END) 'Or that? ' 
FROM Results a 
     INNER JOIN Answers b 
      ON a.id = b.fk_result_id 
     INNER JOIN Question c 
      ON b.fk_question_id = c.ID 
GROUP BY a.ID, 
     a.user_ID, 
     a.job_id 

Si vous avez nombre de questions Unknow (spécifiquement 1000 comme Matei Mihai dit), une version dynamique est beaucoup plus nécessaire.

SET @sql = NULL; 
SELECT 
    GROUP_CONCAT(DISTINCT 
    CONCAT(
     'MAX(CASE WHEN c.question = ''', 
     question, 
     ''' then b.answer end) AS ', 
     CONCAT('`',question,'`') 
    ) 
) INTO @sql 
FROM Question; 

SET @sql = CONCAT('SELECT a.ID, 
          a.user_ID, 
          a.job_id, ', @sql, ' 
        FROM Results a 
          INNER JOIN Answers b 
           ON a.id = b.fk_result_id 
          INNER JOIN Question c 
           ON b.fk_question_id = c.ID 
        GROUP BY a.ID, 
          a.user_ID, 
          a.job_id'); 

PREPARE stmt FROM @sql; 
EXECUTE stmt; 
DEALLOCATE PREPARE stmt; 

SORTIE

╔════╦═════════╦════════╦═════════════╦══════════╦══════════╗ 
║ ID ║ USER_ID ║ JOB_ID ║ IS IT THIS? ║ OR THIS? ║ OR THAT? ║ 
╠════╬═════════╬════════╬═════════════╬══════════╬══════════╣ 
║ 1 ║  1 ║  1 ║ Yes   ║ No  ║ Maybe ║ 
║ 2 ║  1 ║  3 ║ Maybe  ║ No  ║ Maybe ║ 
║ 3 ║  2 ║  3 ║ Yes   ║ Yes  ║ No  ║ 
╚════╩═════════╩════════╩═════════════╩══════════╩══════════╝ 
+0

s'il y a 1000 questions? :) –

+1

@MateiMihai vous pouvez répondre à 1000 questions sur la requête dynamique ci-dessus .. –

+1

Grande réponse .. :) Je suis assez sûr qu'il pourrait y avoir plus de 3 questions dans son tableau .. c'est pourquoi j'ai demandé une requête dynamique. +1 de moi –

Questions connexes