2015-02-05 77 views
0

我需要安排具有某些奇怪條件的表中的值。
我的表是:
| ref | value1 | info1 | value2 | info2 | value3 | info3 |
| 9 | 100 | test1 | 220 | test4 | 300 | test7 |
| 3 | 150 | test2 | 250 | test5 | 400 | test8 |
| 7 | 200 | test3 | 290 | test6 | 500 | test9 |mySQL:合併同一個表中的列,並將值保存在同一行中

我需要能夠得到一個結果,例如:
| ref | values | infos |
| 9 | 100 | test1 |
| 3 | 150 | test2 |
| 7 | 200 | test3 |
| 9 | 220 | test4 |
| 3 | 250 | test5 |
| 7 | 290 | test6 |
| 9 | 300 | test7 |
| 3 | 400 | test8 |
| 7 | 500 | test9 |

我甚至不能開始明白,如果我需要什麼是可能的,我不能讓我的頭周圍。任何幫助將不勝感激。

回答

2

你可以使用UNION ALL

SELECT ref, value1 as values, info1 as Infos FROM tbl 
UNION ALL 
SELECT ref, value2 as values, info2 as Infos FROM tbl 
UNION ALL 
SELECT ref, value3 as values, info3 as Infos FROM tbl 
+0

謝謝你,這完美地工作! – Eduardo 2015-02-05 17:36:11

+0

不客氣Eduardo。快樂編碼和上帝保佑。 – Edper 2015-02-05 17:46:28

相關問題