2013-04-25 69 views
2

我不會粘貼整個查詢。它看起來像這樣。MySQL按列排序= x,列asc?

SELECT *, MONTH(date_created) as month 
from table 
GROUP BY month ORDER BY month='3', month asc 

由於這是今年4月和我查詢我本來期望的順序如下3,1,2,4

相反,我走出1,2,4,3

如何更改聲明的ORDER BY部分,以便按照選定月份的順序排序結果,然後按順序顯示年份中的剩餘月份?

+0

也許這:http://stackoverflow.com/questions/2175439/mysql-custom-sort – 2013-04-25 14:01:30

+0

請停止使用像這樣的「GROUP BY」。這不是ANSI標準。 – Kermit 2013-04-26 02:07:49

回答

5

添加DESC

ORDER BY month = '3' DESC, month asc 

month='3'是一個布爾表達式,其返回1或0,所以基本上當結果是零,它將對結果的最後部分。

使用或不使用DESC,使用<>

ORDER BY month <> '3', month asc 
+0

是的,很好!我比較慢... – JoDev 2013-04-25 14:02:41

0

您必須添加DESCASC第一順序:

SELECT *, MONTH(date_created) as month 
FROM table 
GROUP BY month ORDER BY month='3' DESC, month ASC 
-2

的解決方案是按字段

SELECT * FROM table 
GROUP BY month 
ORDER BY FIELD(month, 3,1,2,4)