如何在Mysql / Sql中按时间列对表进行排序?

时间:2014-06-09 05:58:31

标签: mysql sql

Mysql 5

表名是时间表。

departure_time data_type = varchar(20)

查询

select * from schedules;

输出:

+-------------+----------------+-----------+----------------+-------------+--------+-----------------+
| travel_date | departure_time | origin_id | destination_id | operator_id | status | available_seats |
+-------------+----------------+-----------+----------------+-------------+--------+-----------------+
| 2014-06-09  | 02:30 PM       |       134 |            251 |           2 | Active |              44 |
| 2014-06-09  | 09:30 PM       |       134 |            251 |           2 | Active |              14 |
| 2014-06-09  | 10:00 PM       |       134 |            251 |           2 | Active |              24 |
| 2014-06-09  | 12:30 PM       |       134 |            251 |           2 | Active |              23 |
| 2014-06-09  | 11:15 PM       |       134 |            251 |           2 | Active |              27 |
| 2014-06-09  | 09:30 PM       |       134 |            251 |           4 | Active |              24 |
+-------------+----------------+-----------+----------------+-------------+--------+-----------------+



SELECT * FROM schedules 
WHERE (travel_date ='2014-06-09' and origin_id ='134' 
and destination_id ='251' and operator_id not in (SELECT id FROM operators WHERE (status != 'Active')) and status ='Active' and available_seats > 0) 
ORDER BY departure_time ASC;

输出:

+-------------+----------------+-----------+----------------+-------------+--------+-----------------+
| travel_date | departure_time | origin_id | destination_id | operator_id | status | available_seats |
+-------------+----------------+-----------+----------------+-------------+--------+-----------------+
| 2014-06-09  | 02:30 PM       |       134 |            251 |           2 | Active |              44 |
| 2014-06-09  | 09:30 PM       |       134 |            251 |           2 | Active |              14 |
| 2014-06-09  | 09:30 PM       |       134 |            251 |           4 | Active |              24 |
| 2014-06-09  | 10:00 PM       |       134 |            251 |           2 | Active |              24 |
| 2014-06-09  | 11:15 PM       |       134 |            251 |           2 | Active |              27 |
| 2014-06-09  | 12:30 PM       |       134 |            251 |           2 | Active |              23 |
+-------------+----------------+-----------+----------------+-------------+--------+-----------------+

此处如何通过departure_time asc

生成此表顺序

例如:departure_time应该是这样的: - 12:30 PM,02:30 PM,09:30 PM,09:30 PM,10:00 PM,11:15 PM

2 个答案:

答案 0 :(得分:2)

按照这样的声明更改您的订单,这将有助于

  ORDER BY STR_TO_DATE(timeField,'%h.%i%p');

数据类型TIME用于存储时间数据类型 - 这意味着没有AM / PM。将数据以24小时格式存储在您的数据库中,并使用以下方法之一将其格式化为12小时格式,使用am / pm(PHP或MySQL):

PHP:

$date = new DateTime($mysql_column['time']);
$date->format('h:i:s a');

或:

$date = date('h:i:s a', strtotime($mysql_column['time']));
or MySQL:

SELECT DATE_FORMAT('%h:%i:%s%p',time)FROM table;

答案 1 :(得分:0)

它的类型是varchar(20),所以它将被排序为简单的字符串。