Oliver Spryn
Oliver Spryn

Reputation: 17368

SQL Sort Month Ascending and Months Descending?

Is it possible if I have a table like this:

CREATE TABLE `Fun` (
  `Date` DATETIME NOT NULL PRIMARY KEY
);

to perform an SQL query where the results are sorted like this:

2013-03-01
2013-03-03
2013-03-04
2013-02-11
2013-02-28
2013-01-21
2012-12-10
2012-12-25
2010-07-08

Note that, in the list above, the months and years are sorted in descending order, but the days within each month are sorted in ascending order?

Thank you for your time.

Upvotes: 1

Views: 9626

Answers (1)

John Woo
John Woo

Reputation: 263853

Take advantage of YEAR, MONTH function.

SELECT  *
FROM    Fun
ORDER   BY YEAR(date) DESC, MONTH(date) DESC,
        DATE ASC

Upvotes: 8

Related Questions