MYSQL-订单时间戳记值从最新到最旧的顺序递增? [英] 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.
因此,请解释一下,假设有3个结果:
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
我得到:
2012-07-11 17:34:57
2012-07-11 17:33:07
2012-07-11 17:33:28
这是按数值排序的,07
在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?
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
推荐答案
您的查询:
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
但是您的sqlbox中的结果显示为:
But results in your sqlbox shows :
2012-07-11 17:34:57
2012-07-15 17:33:07
2012-07-15 17:33:28
哪个是完全正确的.
您的发帖中是否有错字?
如果否,请尝试以下操作:
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;
这篇关于MYSQL-订单时间戳记值从最新到最旧的顺序递增?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!