簡體   English   中英

如何在Mysql / Sql中按時間列對表進行排序?

[英]How to sort a table by time column in 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

按照這樣的說法更改您的訂單會有所幫助

  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;

它的類型是varchar(20)因此它將被排序為簡單的字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM