MYSQL - 按从最新到最旧的顺序升序排列时间戳值? [英] MYSQL - Order timestamp values ascending in order, from newest to oldest?

查看:45
本文介绍了MYSQL - 按从最新到最旧的顺序升序排列时间戳值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试按时间戳值对某些结果进行排序时遇到了问题.

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

这是按数值排序的,0728之前.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆