2016-08-18 29 views
3

我有一張表,其中有4列int類型。從所選行中訂購列值

col1 col2 col3 col4 
    3  2  3  4 
    2  2  4  3 

我試圖像下令形式來選擇這些值:

colx colx colx colx 
    2  3  3  4 

列名的結果並不重要,也有結果作爲一個山坳受到歡迎(如果級聯以某種方式):

colx 
2.3.3.4 

感謝

+0

你的問題不明確。你想要從升序或降序的信息?這是你的問題嗎? –

+0

很明顯,因爲我展示了我想要的東西,但無論升序還是降序都不受歡迎 –

+0

您想基於列而不是按行進行排序嗎? Afaik這不是SQL處理的東西,但你可以透視他們檢查這個問題:http://stackoverflow.com/questions/13944417/mysql-convert-column-to-row-pivot-table – Fma

回答

3

對於單列它可能與當前海峽完成但是,如果您有多行需要主鍵或任何唯一鍵。

考慮以下

mysql> select * from test ; 
+------+------+------+------+------+ 
| col1 | col2 | col3 | col4 | id | 
+------+------+------+------+------+ 
| 3 | 2 | 3 | 4 | 1 | 
| 4 | 7 | 1 | 3 | 2 | 
+------+------+------+------+------+ 

現在查詢是作爲

select 
id, 
group_concat(x.col order by x.col separator '.') as colx 
from ( 
    select id,col1 as col from test 
    union all 
    select id,col2 as col from test 
    union all 
    select id,col3 as col from test 
    union all 
    select id,col4 as col from test 
)x group by id 

結果會像

+------+---------+ 
| id | colx | 
+------+---------+ 
| 1 | 2.3.3.4 | 
| 2 | 1.3.4.7 | 
+------+---------+ 
+0

這真是個好主意。謝謝 –