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,下午02:30,晚上9:30,晚上9:30,晚上10:00,晚上11:15
发布于 2014-06-09 06:02:50
按这样的语句更改您的订单会有帮助
ORDER BY STR_TO_DATE(timeField,'%h.%i%p');数据类型时间是用来存储时间数据类型的-这意味着没有AM/PM。将数据以24小时格式存储在数据库中,并在PHP或MySQL中使用am/pm将其格式化为12小时格式,使用以下方法之一:
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:从表中选择DATE_FORMAT('%h:%i:%s %p',time);
发布于 2014-06-09 06:01:40
它的类型是varchar(20),所以它将被排序为简单的字符串。
https://stackoverflow.com/questions/24114446
复制相似问题