Home > Net >  Records set order by to today's date and time in MySql
Records set order by to today's date and time in MySql

Time:09-28

I need to show the records > today's at top and then all future records at bottom. oderby date and time. "CETTime >= CURTIME()" need to only show of today's records.

Is there any suggestion do this with single query. ???

I tried this one but this effect to "AND CETTime >= CURTIME() " future recods times as well , what I need this should be effect only today recods ..

SELECT *, DATEDIFF(`DateofDeparture`, CURDATE()) AS diff   FROM `mytable` 
where DateofDeparture >= CURDATE()  AND
CETTime >= CURTIME() ORDER BY ABS(diff),`CETTime`;
  AND CETTime >= CURTIME()

this condition should be effect only today recods time.

enter image description here

CodePudding user response:

From the result sample you are showing I conclude:

  • you don't want any rows with past dates in the result
  • you want today's rows first in ascending order
  • you want future rows last in ascending order

Use a CASE expression to get today's rows first.

select *, datediff(dateofdeparture, curdate()) as diff
from mytable 
where dateofdeparture >= curdate()
order by case when dateofdeparture = curdate() then 1 else 2 end,
         dateofdeparture,
         cettime;

If you want past / present / future to depend on now rather than today then change the WHERE clause to

where timestamp(dateofdeparture, cettime) >= now()
  • Related