问题
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