I have a simple table, something like: int id, date created_at, date updated_at
. I'd like to order the rows so that any rows that have an updated_at
will be sorted by that, and any that don't will be sorted by the created_at
. The problem is that something like:
SELECT * FROM table ORDER BY updated_at, created_at
doesn't work. I've been looking at custom order clauses, something like:
ORDER BY CASE WHERE updated_at ISNULL <do something> END, created_at
but can't seem to get anything to work. Help?
EDIT:
I should have specified that I want the updated_at
fields to sort before the created_at
fields. So, if the data looks like:
id created_at updated_at
-- ---------- ----------
1 2009-01-08 null
2 2009-09-08 null
3 2009-07-02 null
4 2009-09-05 2009-09-06
5 2009-04-01 null
6 2009-09-07 2009-09-08
I'd want results like:
id created_at updated_at
-- ---------- ----------
6 2009-09-07 2009-09-08
4 2009-09-05 2009-09-06
2 2009-09-08 null
3 2009-07-02 null
5 2009-04-01 null
1 2009-01-08 null