MYSQL order by time am/pm

2019-02-10 15:56发布

I have a row in a table which holds the time of day in am/pm format e.g.

timeField
---------
9.00am
10.00pm
7.00am 
etc...

Is there a way in mysql to order these values?

3条回答
Summer. ? 凉城
2楼-- · 2019-02-10 16:37

You can do this by using STR_TO_DATE function in MySQL:

SELECT STR_TO_DATE('10.00pm','%h.%i%p');

try this:

SELECT * 
FROM table_name 
ORDER BY STR_TO_DATE(timeField,'%h.%i%p');

Example: SQLFiddle

查看更多
Lonely孤独者°
3楼-- · 2019-02-10 16:42

In order to order it you need to format the date field fist.

try this

SELECT COl1, COl2, DATE_FORMAT(yourdate, '%k:%i:%s') AS mydate
FROM your_table
ORDER BY mydate 

Enjoy

查看更多
走好不送
4楼-- · 2019-02-10 16:42
SELECT * FROM table ORDER BY Name

This put everything in order for me: Alphabetized, Dates and Time

查看更多
登录 后发表回答