views:

175

answers:

4

Hello.

How do I use SQL to order results by oldest first? I am using unix timestamps.

Thanks.

+3  A: 

Unix time, or POSIX time, is a system for describing points in time, defined as the number of seconds elapsed since midnight proleptic Coordinated Universal Time (UTC) of January 1, 1970, not counting leap seconds

The ORDER BY clause can use ASC or DESC, if you sepcify none it will default to use ASC:

Most recent time stamps first:

SELECT * FROM tableName ORDER BY columnName DESC

Less recent time stamps first:

SELECT * FROM tableName ORDER BY columnName ASC
Brian R. Bondy
UNIX timestamps are seconds since epoch. The oldest ones have the smallest seconds. What sort of mass hysteria is causing people to say `DESC` here instead of `ASC`?
jemfinch
@jemfinch: You're right, I was thinking along the lines of oldest meaning bigger number. But you are right that an older date is a smaller timestamp. Corrected.
Brian R. Bondy
Sort of reverse occam's razor: if it's worth asking a question about this, it can't just be a SELECT * FROM ... ORDER BY. ...Can it?
rjh
@RJH: You obviously haven't seen some of the questions here.
SLaks
A: 

Use ORDER BY clause with DESC modifier that reverses the results:

SELECT ... FROM ... ORDER BY timestampCol DESC;

Edit

Of course you should use ASC (or none, cause ASC is default)... ;)

Crozin
+1  A: 

What's the problem you're having using ORDER BY 'unix-time-stamp-field' ASC;?

EDIT: jemfinch is right, it is ASC.

Ben
+6  A: 

The oldest UNIX timestamp is the one that's smallest, so you want to ORDER BY my_timestamp_column ASC.

I have no idea why both the answers so far have said to order by the column DESC.

jemfinch
+1, even with a date/datetime field it would be ASC.
rjh
I have to say I have no idea why did I proposed `DESC`... maybe because it's 3 AM, but... ;)
Crozin
It happens to the best of us :)
jemfinch