MySql - order by monthname

与世无争的帅哥 提交于 2019-12-10 14:33:14

问题


I am trying to order mysql query by month name like:

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

Here is my query, but its not ordering:

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

Kindly tell me where I am doing wrong


回答1:


you can specify like

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');



回答2:


try using the built in month function to get the month's number and order by that. For example:

order by month(leave_balance.date_added)



回答3:


Just add at the end:

ORDER BY str_to_date(MONTH,'%M')



回答4:


simply order by leave_balance.date_added field. By default it will sort by month.



来源:https://stackoverflow.com/questions/15607516/mysql-order-by-monthname

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!