我有一个包含datetime字段类型的表,我想对select语法进行排序依赖于它的降序,如下所示:
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
答案 0 :(得分:26)
ORDER BY columnname DESC
<强>钽DAH!强>
答案 1 :(得分:12)
ORDER BY UNIX_TIMESTAMP(columnname) DESC
需要UNIX_TIMESTAMP
,因为mysql将它们作为字符串进行测试(9:00
之后出现16:00
}
答案 2 :(得分: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;