MySql - Order by date and then by time

If you want to list out data as latest eventdate first, if records of same date exists then data will be sorted by time ie latest time first,,

You can try as below

select * from my_table order by eventDate DESC, eventHour DESC

I'm not sure whether there's any hidden meaning in your question but the standard way of doing this seems to fit:

... order by eventDate, eventHour

That gives you hours within dates, like:

Feb 15
    09:00
    12:00
    17:00
Feb 23
    22:00
    : :

If you actually have those two fields as real datetime fields, your schema is screwed up. You should have a date field for the date and a time or integral field for the hour.

You could combine both into a single datetime field but you should balance that against the inefficiencies of doing per-row functions in your select statements if you want to separate them. It's usually better to keep fields separate if your going to use them distinctly.

If you do have to use per-row functions, you can use:

date(datetime_column)
time(datetime_column)

to extract just the date and time components of a datetime.


Does

select * from my_table order by eventDate, eventHour

not work?


Why not save both the eventDate and eventHour into the same field if they are both DateTime ?

To get what you want with the current scheme you can do this:

SELECT * FROM table 
  ORDER BY 
    EXTRACT(YEAR_MONTH FROM eventDate),
    EXTRACT(DAY FROM eventDate),
    EXTRACT(HOUR FROM eventHour)