Reputation: 905
How do I use SQL to order results by oldest first? I am using unix timestamps.
Thanks.
Upvotes: 3
Views: 6019
Reputation: 11
In order to get oldest first data when using unix_timetamp
, run this query:
Select * FROM tablename order by FROM_UNIXTIME(ts) ASC
here:
ts respond to column of the table which has unix_timestamp
.
Upvotes: 1
Reputation: 347216
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
Upvotes: 3
Reputation: 16543
What's the problem you're having using ORDER BY 'unix-time-stamp-field' ASC;
?
EDIT: jemfinch
is right, it is ASC
.
Upvotes: 1
Reputation: 44376
Use ORDER BY
clause with DESC
modifier that reverses the results:
SELECT ... FROM ... ORDER BY timestampCol DESC;
Of course you should use ASC
(or none, cause ASC
is default)... ;)
Upvotes: 0
Reputation: 2889
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
.
Upvotes: 8