简体   繁体   English

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

[英]How to sort a table by time column in Mysql/Sql?

Mysql 5 Mysql 5

Table name is schedule. 表名是时间表。

departure_time data_type = varchar(20) departure_time data_type = varchar(20)

query 询问

select * from schedules;

Output: 输出:

+-------------+----------------+-----------+----------------+-------------+--------+-----------------+
| 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;

output: 输出:

+-------------+----------------+-----------+----------------+-------------+--------+-----------------+
| 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 |
+-------------+----------------+-----------+----------------+-------------+--------+-----------------+

Here how to make this table order by departure_time asc 这里如何通过departure_time asc生成此表顺序

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

Change your order by statement like this it will help 按照这样的说法更改您的订单会有所帮助

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

Data Type TIME is for storing time data type - that means no AM/PM. 数据类型TIME用于存储时间数据类型 - 这意味着没有AM / PM。 Store the data in Your database in 24 hour format and format it to 12 hour format with am/pm in PHP or MySQL using one of these: 将数据以24小时格式存储在您的数据库中,并使用以下方法之一将其格式化为12小时格式,使用am / pm(PHP或MySQL):

PHP: PHP:

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

or: 要么:

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM