2012-09-21 45 views
3

我有一個數據集是這樣的:MySQL的切片訂貨

boolean name value 
0  Text10 20 
1  Text1 8 
0  Text4 46 
1  Text9 84 
1  Text5 66 
0  Text2 35 
0  Text9 2 
1  Text6 55 

排序由boolean列會分裂,我想根據每一個不同的參數來訂購兩個部分數據:與boolean = 1的那些是按值排序,其餘是由name有序的,就像這樣:

boolean name value 
1  Text1 8  # --> 1s are ordered by value 
1  Text6 55 
1  Text5 66 
1  Text9 84 
0  Text2 35 # --> 0s are ordered by name 
0  Text4 46 
0  Text9 2 
0  Text10 20 

注:我們需要這在MySQL 4.1.11工作。 = D

+1

4.1.11 OMG!所以對不起你的男人.... – Ray

+0

謝謝@Ray,但我很抱歉。 = p – vmassuchetto

回答

9

請注意,它可以用order by的多個部分完成,根據需要「激活」。
您可以檢查此sqlfiddle

select * from yourtable 
order by 
    boolean desc, 
    case when boolean = 0 then value else null end, -- ´else null´ is redundant 
    case when boolean = 1 then name else null end -- but is here to clarify 
+2

給這個人勾選標記。 – bobwienholt

+0

花了我一些時間回到這裏,@ bobwienholt。 = d – vmassuchetto