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?

1

1 Answer 1

4

simply do:

ORDER BY GREATEST(date_created, date_saved, date_published, date_deleted)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.