13

我有一个包含日期时间字段类型的表,我想对选择语法进行排序取决于它的降序,如下所示:

2012-12-12 01:44:20

2012-12-11 01:44:40

2012-12-10 01:40:36

2012-12-09 12:28:19

2012-12-09 12:19:21

2012-12-09 12:11:50

2012-12-09 12:00:11

2012-12-09 11:59:26

2012-12-09 11:57:08

2012-12-09 11:55:21

4

3 回答 3

31
ORDER BY columnname DESC

达达!

于 2012-12-16T08:33:05.110 回答
22
ORDER BY UNIX_TIMESTAMP(columnname) DESC

UNIX_TIMESTAMP是必需的,因为 mysql 将它们作为字符串进行测试(例如出现9:00在 之后16:00

于 2014-06-02T21:03:46.793 回答
0

在我的情况下dtEvent字段是日期时间类型。这是我找到的唯一解决方案:

SELECT myTable.recordId, myTable.dtEvent
FROM myTable
WHERE myTable.dtEvent IS NOT NULL
ORDER BY CAST(YEAR(myTable.dtEvent) AS UNSIGNED),
    CAST(MONTH(myTable.dtEvent) AS UNSIGNED),
    CAST(DAY(myTable.dtEvent) AS UNSIGNED) 
LIMIT 100;
于 2018-02-16T09:54:40.167 回答