May 20, 2009

Order By Date in MySQL

Suppose you write a query to sort a table by date field.

SELECT aDate FROM aTable ORDER BY aDate 

The above query should sort aDate column in ascending order. But it doesnt work the way it should.
To sort date field in MySQL use following query:

SELECT DATE_FORMAT(aDate, '%d-%M-%Y') AS aDate, aDate AS sDate FROM aTable ORDER BY sDate 

 

Click Here!