17

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
14
  • Your result set can't be right. 2012-07-11 17:34:57 will never come before 2012-07-11 17:33:07 when ordered ascending.
    – user610217
    Commented Jul 15, 2012 at 16:14
  • Check the other parts of the time stamp. Are they identical? Looks like you have overlooked something
    – Dojo
    Commented Jul 15, 2012 at 16:16
  • sqlfiddle.com/#!2/421ce/1 Something doesn't feel right.
    – hjpotter92
    Commented Jul 15, 2012 at 16:18
  • Additionally, your descending order (want to be like this) is not possible, either.
    – user610217
    Commented Jul 15, 2012 at 16:18
  • 1
    @BlackberryFan, put the backticks on your timestamp column, you are probably ordering by the reserved keyword. You can tell this is not right because :34 will always come after :33, so ignoring the :07 vs :28 issue, other things are wrong.
    – user610217
    Commented Jul 15, 2012 at 16:25

4 Answers 4

23

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;
0
4

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

Show create table tablename;
3
  • Almost definitely; problem is, even for a string it is not ordered correctly.
    – user610217
    Commented Jul 15, 2012 at 16:22
  • Ray, thanks for your reply, I have edited my answer to include the requested info. It is the full output of what you have requested. Commented Jul 15, 2012 at 16:29
  • @BlackberryFan See the answer below someone else put... looks like you just miss read the timestamps based on your screenshot
    – Ray
    Commented Jul 15, 2012 at 17:29
1

if you write the query as:

select q.`timestamp`
from user_quotations as q
order by q.`timestamp`
limit 30

you should have them ordered properly.

If not, there is a problem with the timestamp data. Look for leading/ trailing spaces, odd characters, etc.

3
  • Thank you so much for your reply and your effprt, but this still returns the same result set. Could this not have something to do with the date format I have used as it is not the standard? Or possibly the tables charset, although i dont see how this could affect the timestamp field. Commented Jul 15, 2012 at 16:33
  • regardless of the date format, if it's a datetime field, it should always order correctly. It's possible your browser/ phpMyAdmin is caching your results, but there is no way (unless you have a truly broken MySQL instance) that you should ever see the columns ordered that way with the above query.
    – user610217
    Commented Jul 15, 2012 at 16:35
  • It is the timestamp field as I did specify. There is a datetime field but it is not the one that I am requesting results from. Commented Jul 15, 2012 at 16:37
1

screen shot shows two of the results from 15th and one from 11th. Probably affects the order a wee bit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.