Wouter Vervloet
Wouter Vervloet

Reputation: 131

Sort rows by multiple columns

A the title says, I'm looking for a way to sort a set of rows from a MySQL table.

I've simplified my example a bit, but the resulting solution should apply to my exact setup as well.Here's my setup:

events
----------
event_name
created
updated


Event A 
12-09-2012
14-09-2012

Event B
12-09-2012
-

Event C
09-09-2012
13-09-2012  

I'm looking for a way to sort the rows based on most recent 'activity', so whichever date is the newest. So with the above values, the result should be A, C, B. (from most recent to older). It is worth noting that I can't change anything about the tables.

How would I go around doing this?

Regards, Wouter

Upvotes: 1

Views: 177

Answers (2)

Wouter Vervloet
Wouter Vervloet

Reputation: 131

I think I might be on to something here...

SELECT *, GREATEST(created, updated) AS last_activity FROM events ORDER BY last_activity DESC

This does seem to work, but I'll have to do some testing to see if it is foolproof.

Upvotes: 2

codingbiz
codingbiz

Reputation: 26386

Try this, should be this simple

 SELECT event_Name From Events
 ORDER created DESC

Upvotes: 0

Related Questions