gokul
gokul

Reputation: 49

How to sort the time in table using my sql query

how to sort the table for time . that is from 6.00 AM t0 10.00 PM

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

I tried this query in side subquery still not working

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' 

Upvotes: 1

Views: 193

Answers (1)

Code Lღver
Code Lღver

Reputation: 15603

use this query:

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

SQL FIDDLE working example.

EDITED:

After seeing your query i am posting the modified query for you.

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

Upvotes: 3

Related Questions