MySql - 按月份名称排序

时间:2013-03-25 04:13:54

标签: mysql sql-order-by

我正在尝试按月名称命令mysql查询:

January --  5
February -- 2
March    -- 5
and so on

这是我的查询,但不是订购:

SELECT leave_balance.balance, MonthName(leave_balance.date_added) AS month 
FROM leave_balance WHERE leave_balance.staff_id_staff = $iid 
GROUP BY month,  leave_balance.leave_type_id_leave_type 
HAVING leave_balance.leave_type_id_leave_type = $leaveBalTypID 
ORDER BY month

请告诉我我做错了什么

4 个答案:

答案 0 :(得分:11)

您可以指定

ORDER BY FIELD(MONTH,'January','February','March',...)


SELECT leave_balance.balance, MonthName(leave_balance.date_added) AS month 
FROM leave_balance WHERE leave_balance.staff_id_staff = $iid 
GROUP BY month,  leave_balance.leave_type_id_leave_type 
HAVING leave_balance.leave_type_id_leave_type = $leaveBalTypID 
ORDER BY FIELD(MONTH,'January','February','March',...,'December');

答案 1 :(得分:2)

尝试使用内置月份函数来获取月份的数量和顺序。例如:

order by month(leave_balance.date_added)

答案 2 :(得分:1)

最后添加:

ORDER BY str_to_date(MONTH,'%M')

答案 3 :(得分:0)

只需按leave_balance.date_added字段排序即可。默认情况下,它将按月排序。