MySQL datetime字段查询按小时:分钟排序

时间:2013-02-10 11:40:16

标签: mysql sql sorting mysqldatareader

我有一张这样的表:

id  date_time
1   2/11/2013 7:05
2   2/11/2013 7:00
3   2/12/2013 7:00
4   2/14/2013 7:00
5   2/16/2013 7:00
6   2/17/2013 7:00
7   2/12/2013 7:05
8   2/14/2013 7:05
9   2/15/2013 7:05
10  2/16/2013 7:05
11  2/17/2013 7:05
12  2/13/2013 7:00
13  2/15/2013 7:00
14  2/13/2013 7:05

我需要它按HOUR:MINUTE排序,然后按DATE排序,所以我在输出上得到这样的东西:

2/11/2013 7:00
2/12/2013 7:00
2/13/2013 7:00
2/14/2013 7:00
2/15/2013 7:00
2/16/2013 7:00
2/17/2013 7:00
2/11/2013 7:05
2/12/2013 7:05
2/13/2013 7:05
2/14/2013 7:05
2/15/2013 7:05
2/16/2013 7:05
2/17/2013 7:05

有没有办法直接用MySQL对输出进行排序?...我知道一旦得到查询结果就通过PHP来做,但只是想知道MySQL是否可以做类似的事情?

我尝试了这样的查询:

SELECT * FROM my_table WHERE (date_time BETWEEN '$date_check_low' AND '$date_check_high') ORDER BY hour(date_time) ASC

但它会产生奇怪的结果......

2 个答案:

答案 0 :(得分:3)

ORDER BY HOUR(date_time), MINUTE(date_time), date_time

ORDER BY TIME(date_time), date_time

答案 1 :(得分:0)

试试这个:

SELECT *
FROM my_table
WHERE (date_time BETWEEN '$date_check_low' AND '$date_check_high')
ORDER BY time(date_time),
         date_time;