Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ORDER By Unix Timestamp DESC or ASC?

I have news items that when created adds the time of creation date/time in unix timestamp format into the database. If i wanted to order by most recent first would i use ASC or DESC in my mysql query?

EDIT:

Thanks everyone for replying. I understand now. I will make Sarfraz answer the accepted solution as he was first to reply but thanks to everyone else to :) . Have to wait 11 minutes before i can accept it as a solution.

like image 207
PHPLOVER Avatar asked Feb 16 '11 19:02

PHPLOVER


People also ask

What is ASC vs DESC?

You can use the ASC and DESC keywords to specify ascending (smallest value first) or descending (largest value first) order. The default order is ascending. For DATE and DATETIME data types, smallest means earliest in time and largest means latest in time.

Can you order by timestamp SQL?

You can use ORDER BY ASC to order timestamp values in ascending order with TIMESTAMP() method.

How do I sort newest to oldest in SQL?

The ORDER BY command is used to sort the result set in ascending or descending order. The ORDER BY command sorts the result set in ascending order by default. To sort the records in descending order, use the DESC keyword.


2 Answers

DESC - timestamps are "higher = newer" number. So sorting by DESC(ending) will put the highest (newest) entries first.

like image 119
Marc B Avatar answered Oct 23 '22 06:10

Marc B


You should perform a SQL Query like below

SELECT * FROM News ORDER BY date DESC
like image 25
dextervip Avatar answered Oct 23 '22 06:10

dextervip