Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql order items by the newest of 2 dates

I have a table with 2 dates - the ADDDATE and UPDATEDATE.
When an item is added, the ADDDATE is the current date and the UPDATEDATE is 0000-00-00.

I need to ORDER BY the SQL command and get the best date of those 2.
For example if ADD=12/1/2010 and UPDATE=30/6/2010, the UPDATE DATE is best (newest).

Any advice?

like image 775
ntan Avatar asked Jul 16 '10 12:07

ntan


1 Answers

Use GREATEST to find the newest date:

ORDER BY GREATEST(UPDATEDATE, ADDDATE)
like image 151
Mark Byers Avatar answered Sep 27 '22 21:09

Mark Byers