I am currently experiencing a problem with sorting a result by a data column which is using date_format.
I have the below dates:
- 12-03-12
- 21-03-12
- 25-03-12
- 17-04-12
When I perform the query:
SELECT date FROM myTable ORDER date DESC
The dates are ordered in the correct order
- 17-04-12
- 25-03-12
- 21-03-12
- 12-03-12
When I perform the query
SELECT DATE_FORMAT(date, '%d-%m-%Y') as `date` ORDER BY date
The dates are now in the wrong order
- 25-03-12
- 21-03-12
- 17-04-12
- 17-03-12
- 14-03-12
I’ve also tried running the query
SELECT DATE_FORMAT(date, ‘%d-%m-%Y’) as date ORDER BY DATE_FORMAT(date, ‘%d-%m-%Y’) but has made no difference.
How can I get this to sort in the correct order.
The problem is that you are overriding the column name with the alias.
Choose another alias: