如何使用我的SQL查询对表中的时间进行排序

时间:2013-09-27 10:50:12

标签: mysql sql

如何对表格进行排序。那是从早上6点到晚上10点

 name    source destination time 
 gokul   xxx    yyyy        10.00 PM
 abc     xxx    yyyy        6.00 AM

我在副子查询中尝试此查询仍然无法正常工作

Select * from (
  Select * from trips where date = '27-09-2013' 
  and time like '%PM' or '%pm' ORDER BY time
) AND (
  Select * from trips where date = '27-09-2013' 
  and time like '%AM' or '%am' ORDER BY time
) AS TIME 
ORDER BY TIME(time) DESC' 

1 个答案:

答案 0 :(得分:3)

使用此查询:

SELECT * FROM tabel_name 
    ORDER BY STR_TO_DATE(`time`,'%h.%i%p');

SQL FIDDLE工作示例。

<强>编辑:

在看到您的查询后,我将为您发布修改过的查询。

SELECT * FROM trips 
    WHERE `date` = '27-09-2013' 
       AND ((`time` like '%PM' OR '%pm') 
          OR (`time` like '%AM' OR '%am')) ORDER BY STR_TO_DATE(`time`,'%h.%i%p') DESC