10

我正在尝试按月份名称订购 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

4 回答 4

20

你可以指定像

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');
于 2013-03-25T04:17:34.873 回答
7

只需在查询语句的末尾添加:

ORDER BY str_to_date(MONTH,'%M')

如果将来列名发生更改,您将无需担心。

于 2018-02-08T23:42:45.143 回答
6

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

order by month(leave_balance.date_added)
于 2013-03-25T04:18:06.053 回答
2

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

于 2013-03-25T04:54:29.803 回答