简体   繁体   中英

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

Mysql 5

Table name is schedule.

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

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

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. 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:

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;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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