Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL - Order timestamp values ascending in order, from newest to oldest?

I have come across a problem when trying to order certain results by their timestamp value.

I would like these results displayed from the newest, to the oldest based on the timestamp values.

So to explain this, imagine that there were 3 results:

2012-07-11 17:34:57
2012-07-11 17:33:28
2012-07-11 17:33:07

This result set would be what I would require, but given the following query

SELECT timestamp
FROM randomTable
ORDER BY timestamp ASC

I get:

2012-07-11 17:34:57
2012-07-11 17:33:07
2012-07-11 17:33:28

This is as it is sorted by numerical value and 07 comes before 28.

If i sort in descending order I get

2012-07-11 17:33:07
2012-07-11 17:33:28
2012-07-11 17:34:57

Which is what I am looking for... But it is in reverse.

So my question is fairly simple, how could I sort these values in ascending order as I have described?

EDIT:

The problem

EDIT2:

CREATE TABLE `user_quotations` (
 `id` int(100) NOT NULL AUTO_INCREMENT,
 `quoteNumber` int(100) NOT NULL,
 `lastModified` datetime NOT NULL,
 `userId` int(100) NOT NULL,
 `manufacturer` varchar(250) COLLATE latin1_general_ci NOT NULL,
 `modelNumber` varchar(250) COLLATE latin1_general_ci NOT NULL,
 `productDesc` varchar(1000) COLLATE latin1_general_ci NOT NULL,
 `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
 PRIMARY KEY (`id`),
 KEY `quoteNumber` (`quoteNumber`,`lastModified`,`userId`,`manufacturer`,`modelNumber`,`timestamp`),
 KEY `productDesc` (`productDesc`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci
like image 824
Craig van Tonder Avatar asked Jul 15 '12 16:07

Craig van Tonder


People also ask

How do I arrange ascending order in MySQL?

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

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 SQL by date from oldest to newest?

Use the ORDER BY keyword and the name of the column by which you want to sort. This way, you'll sort the data in ascending order by this column. You could also use the ASC keyword to make it clear that the order is ascending (the earliest date is shown first, the latest date is shown last, etc.).

How can I change the date order in MySQL?

If you'd like to see the latest date first and the earliest date last, you need to sort in descending order. Use the DESC keyword in this case. ORDER BY exam_date DESC ; Note that in MySQL, NULL s are displayed first when sorting in ascending order and last when sorting in descending order.


2 Answers

Your query :

SELECT timestamp
FROM randomTable
ORDER BY timestamp ASC;

is perfect. But I doubt about the results you have presented in your posting. You posted :

2012-07-11 17:34:57
2012-07-11 17:33:07
2012-07-11 17:33:28

But results in your sqlbox shows :

2012-07-11 17:34:57
2012-07-15 17:33:07
2012-07-15 17:33:28

Which are perfectly right.

Is that a typo error in your posting?
If no, then try the following :

SELECT timestamp( `timestamp` ) as 'timestamp'
FROM randomTable
ORDER BY 1 ASC;
like image 187
Ravinder Reddy Avatar answered Oct 23 '22 11:10

Ravinder Reddy


Check your create statement for the table. I expect your timestamp column is really a string.

Show create table tablename;
like image 4
Ray Avatar answered Oct 23 '22 11:10

Ray