Reputation: 39
I have a column in my database that stores the release month of many products, this is stored as a word 'January', 'March' etc...
I need to output the products in the order of month descending however the problem is that because the month is stored as a string my current query is ordering the products by month alphabetically.
Here is my current query
SELECT * FROM models ORDER BY year DESC, month DESC LIMIT 3
Is there possibly a way I can still sort these in descending month order, for example, December, November...
Upvotes: 1
Views: 221
Reputation: 1270793
One method is to use field()
:
order by year desc,
field(month, 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December') desc
Or, transform the value to date:
order by str_to_date(concat(year, '-', month), '%Y-%M')) desc
Upvotes: 1