SQL sorting by date problem
I wanna sort by date; however, after I trimmed of the time part of the date. It doesn't recognize the dates. Any suggestions?
SELECT Count(*) AS total,
DATE_FORMAT(install_date, '%d-%m-%Y') AS new_date
FROM extable
GROUP BY new_date
ORDER BY new_date DESC
----OUTPUT-----
1583 31-12-2010
1180 31-10-2010
64 31-08-2010
959 31-03-2011
1520 31-01-2011
1626 30-12-开发者_如何学Python2010
920 30-11-2010
1608 30-10-2010
DATE_FORMAT is converting your date into a String with the given format. You are then sorting by that String in which case, the DBMS is applying the correct sort. You should sort by install_date instead if you want to sort by date.
You could also sort in the order Y-M-D instead. That would give you the correct sorting.
精彩评论