2

I have a table with 4 different date columns (date_created, date_saved, date_published, date_deleted), and I'm trying to do a "most recent updates" query, effectively taking any of those 4 dates into consideration and sorting by the most recent dates desc. Normally you'd do:

date_created DESC, date_saved DESC, date_published DESC, date_deleted DESC

But I don't want it to give more importance to date_created than date_deleted. All the date fields are an "update" so they should all be treated equally in the sorting.

I'd rather not select all the results in the whole database and then use PHP to sort and limit for resources sake, so, is there a way sort DESC on all of those 4 fields, while treating them all as equal importance?

MattD
  • 179
  • 1
  • 13
  • 1
    `ORDER BY GREATEST(date_created,...)` (or `LEAST`) : http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html, http://stackoverflow.com/questions/19445828/mysql-select-minimum-maximum-among-two-or-more-given-values – a1ex07 Oct 09 '15 at 20:01

1 Answers1

4

simply do:

ORDER BY GREATEST(date_created, date_saved, date_published, date_deleted)
arilia
  • 9,373
  • 2
  • 20
  • 44