Eric Gates
Eric Gates

Reputation: 905

SQL order by oldest from Unix Timestamps

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

Thanks.

Upvotes: 3

Views: 6019

Answers (5)

user3170778
user3170778

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

Brian R. Bondy
Brian R. Bondy

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

Ben
Ben

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

Crozin
Crozin

Reputation: 44376

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)... ;)

Upvotes: 0

jemfinch
jemfinch

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

Related Questions