MYSQL - 按从最新到最旧的顺序升序排列时间戳值?

MYSQL - Order timestamp values ascending in order, from newest to oldest?(MYSQL - 按从最新到最旧的顺序升序排列时间戳值?)
本文介绍了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 - 按从最新到最旧的顺序升序排列时间戳值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)