views:

35

answers:

4

Hi!

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!!

+4  A: 

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.

paxdiablo
Sounds like they only want the date from eventDate and the hour from eventHour though. If the hour of the eventDate wasn't the same for all results on that date then that would be ordered before the hour of the eventHour.
Graphain
Upvote for clarifying the date/time difference for them.
Graphain
+1  A: 

You can order by as many as you need, just use a list. It will order by the first value, then the second...etc.

  SELECT *
    FROM table
ORDER BY eventDate, eventHour

If you have two datetime fields then you can get just the date or just the hour using something like:

    SELECT *
      FROM table
  ORDER BY DATE_FORMAT(eventDate, '%d'), DATE_FORMAT(eventHour, '%h')
northpole
Sounds like they only want the date from eventDate and the hour from eventHour though. If the hour of the eventDate wasn't the same for all results on that date then that would be ordered before the hour of the eventHour.
Graphain
+1  A: 

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)
Graphain
I added the "solution" to their schema, but they should *not* be doing this.
Graphain
OMG Ponies
+2  A: 

Does

select * from my_table order by eventDate, eventHour

not work?

Ned Batchelder
Sounds like they only want the date from eventDate and the hour from eventHour though. If the hour of the eventDate wasn't the same for all results on that date then that would be ordered before the hour of the eventHour.
Graphain