這裏的表:TABLE1劃分爲不同的行表的列與MySQL
ID | id_activity | id_elem | text
---------------------------------------
1 | 1 | 11 | text1 |
2 | 1 | 12 | text2 |
3 | 1 | 13 | text3 |
4 | 2 | 11 | text4 |
5 | 2 | 12 | text5 |
6 | 2 | 13 | text6 |
7 | 3 | 11 | text7 |
8 | 3 | 12 | text8 |
9 | 3 | 13 | text9 |
10 | 4 | 11 | text10 |
11 | 4 | 12 | text11 |
12 | 4 | 13 | text12 |
13 | 5 | 11 | text13 |
14 | 5 | 12 | text14 |
15 | 5 | 13 | text15 |
16 | 6 | 11 | text16 |
17 | 6 | 12 | text17 |
18 | 6 | 13 | text18 |
我需要這樣一個結果:
ID | text_elem_11 | text_elem_12 | text_elem_13
---------------------------------------------------
1 | text1 | text2 | text3 |
2 | text4 | text5 | text6 |
3 | text7 | text8 | text9 |
4 | text10 | text11 | text12 |
5 | text13 | text14 | text15 |
6 | text16 | text17 | text18 |
什麼是做到這一點的正確方法?與後續的查詢,我只能得到一個表與第一和第二列
SELECT table1.ID,
table1.id_elem,
elem_11.text AS text_elem_11
FROM table1
INNER JOIN table1 AS elem_11 ON
table1.id_activity = 1 AND
table1.id_elem = 11 AND
elem_11.id_elem = 11
這是結果
id_activity | id_elem | text_elem_11 |
-----------------------------------------
1 | 11 | text1 |
1 | 11 | text4 |
1 | 11 | text7 |
我不知道如何添加其他2列,如果是一個好主意,用一個單一的查詢來做...所以,任何想法?
考慮在應用層處理數據顯示的問題。這種方式更加靈活。 – Strawberry 2013-05-08 17:18:11