首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Mysql/Sql中按时间列对表进行排序?

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

Stack Overflow用户
提问于 2014-06-09 05:58:30
回答 2查看 1.6K关注 0票数 3

Mysql 5

表名是时间表。

departure_time data_type = varchar(20)

查询

代码语言:javascript
复制
select * from schedules;

输出:

代码语言:javascript
复制
+-------------+----------------+-----------+----------------+-------------+--------+-----------------+
| 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;

产出:

代码语言:javascript
复制
+-------------+----------------+-----------+----------------+-------------+--------+-----------------+
| 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,下午02:30,晚上9:30,晚上9:30,晚上10:00,晚上11:15

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-06-09 06:02:50

按这样的语句更改您的订单会有帮助

代码语言:javascript
复制
  ORDER BY STR_TO_DATE(timeField,'%h.%i%p');

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

PHP:

代码语言:javascript
复制
$date = new DateTime($mysql_column['time']);
$date->format('h:i:s a');

或者:

代码语言:javascript
复制
$date = date('h:i:s a', strtotime($mysql_column['time']));
or MySQL:

从表中选择DATE_FORMAT('%h:%i:%s %p',time);

票数 3
EN

Stack Overflow用户

发布于 2014-06-09 06:01:40

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24114446

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档