ennanj ennanj -4 years ago 94
MySQL Question

mysql query for order base on current time

mysql query for order base on current time
I just want to ask what would be the best query for sorting the time value.

ex. i have 5 records of time data type.


7:00

8:00

9:00

10:00

11:00

I want to sort the record base on the current time.

ex. if its already 9:00,

i want the out put to be :

9:00

10:00

11:00

7:00

8:00


is this possible? if so. what query would be the best approach?
thanks in advance. hope some one could answer my question.

Answer Source

Use a CASE expression in the ORDER BY clause.

Query

SELECT * FROM `your_table_name` 
ORDER BY CASE WHEN `time_column` >= cast(now() as time) then 0 else 1 end, `time_column`;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download