0

我在格式的表格中有一堆日期August 31, 2013, 5:02 am。我需要将这些转换为日期并按日期排序。我正在使用下面的查询,但它以一种奇怪的方式返回日期。

SELECT name, date, link FROM table_name ORDER BY str_to_date(date, '%M %e, %Y, %l %p')

因此,对于一组示例数据,我有:

August 31, 2013, 5:02 am
October 10, 2013, 2:14 pm
October 14, 2013, 7:00 pm
October 22, 2013, 1:46 pm
October 22, 2013, 2:47 pm

但是,当查询运行时,排序发生如下:

August 31, 2013, 5:02 am
October 22, 2013, 2:47 pm
October 22, 2013, 1:46 pm
October 10, 2013, 2:14 pm
October 14, 2013, 7:00 pm

我需要做什么来更正查询并正确显示数据?

4

1 回答 1

2

%l:%i如果您使用时间部分,它将起作用,而不仅仅是%l

mysql> SELECT str_to_date(date, '%M %e, %Y, %l:%i %p') FROM table_name ORDER BY str_to_date(date, '%M %e, %Y, %l:%i %p');
+-----------------------------------------------+
| str_to_date(timestamp, '%M %e, %Y, %l:%i %p') |
+-----------------------------------------------+
| 2013-08-31 05:02:00                           |
| 2013-10-10 14:14:00                           |
| 2013-10-14 19:00:00                           |
| 2013-10-22 13:46:00                           |
| 2013-10-22 14:47:00                           |
+-----------------------------------------------+
5 rows in set (0.00 sec)
于 2013-10-22T19:48:22.097 回答