I’ve got months stored in a table like this “December, August, July”, etc. How can I order my query so that they’re ordered chronologically rather than alphabetically? I probably need to convert from string to time to get a number then order by that, I can do this in PHP but don’t know how to do this directly within the query so I don’t have to make a query, use PHP to manipulate some data, then make another query using that.
Can MySQL do this in a query?
Thanks.
Yes, you will hate this :-
There is another str_to_date function in mysql
mysql> select str_to_date('January', '%M'); +------------------------------+ | str_to_date('January', '%M') | +------------------------------+ | 0000-01-00 | +------------------------------+ --> sorting order by str_to_date(column_name, '%M')Since you not going to provide other information (year,day, time),
so this function will work too.
The best way is convert all the string into small integer,
which is the correct way and have the best optimization