3
我正在努力尋找解決這個MySQL問題的方法。我似乎無法理解如何去做。我有以下表格。MySQL數據透視表列數據作爲行
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 |
+----+-------------------------+--------------+
如果可能,我想顯示問題的答案作爲每個結果集的列,就像這樣。
+-----------+---------+--------+-------------+----------+----------+
| 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 |
+-----------+---------+--------+-------------+----------+----------+
任何幫助將不勝感激。
感謝
如果什麼有1000個問題嗎? :) – 2013-03-27 10:17:20
@MateiMihai你的1000個問題可以照上面的動態查詢.. – 2013-03-27 10:23:09
很好的答案.. :)我敢肯定,他的表中可能會有更多的3個問題..這就是爲什麼我要求一個動態查詢。我的+1 – 2013-03-27 10:25:26