Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySql - Order by date and then by time

I have a table with 2 'datetime' fields: 'eventDate' and 'eventHour'. I'm trying to order by 'eventDate' and then by 'eventHour'.

For each date I will have a list os events, so that's why I will to order by date and then by time.

thanks!!

like image 765
André Miranda Avatar asked Jul 26 '10 01:07

André Miranda


1 Answers

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.

like image 71
paxdiablo Avatar answered Oct 20 '22 13:10

paxdiablo