MySQL date format DD/MM/YYYY select query?

ghz 1years ago ⋅ 9845 views

Question

I'm a bit confused on how to order by date formats.

For the format YYYY-MM-DD you would do this: ...ORDER BY date DESC...

How would you order by DD/MM/YYYY?

This isn't working:

SELECT * FROM $table ORDER BY DATE_FORMAT(Date, '%Y%m%d') DESC LIMIT 14

Answer

You can use [STR_TO_DATE()](http://dev.mysql.com/doc/en/date-and-time- functions.html#function_str-to-date) to convert your strings to MySQL date values and ORDER BY the result:

ORDER BY STR_TO_DATE(datestring, '%d/%m/%Y')

However, you would be wise to convert the column to the DATE data type instead of using strings.