Reputation: 6588
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!!
Upvotes: 17
Views: 83576
Reputation: 1
why not to use a TIMESTAMP to join the both fields?
First, we need to convert the eventDate field to DATE, for this, we'll use the DATE() function:
SELECT DATE( eventDate );
After that, convert the eventHour to TIME, something like this:
SELECT TIME( eventHour );
The next step is to join this two functions into a TIMESTAMP function:
SELECT TIMESTAMP( DATE( eventDate ), TIME( eventHour ) );
Yeah, but you don't need a SELECT, but an ORDER BY, so a complete example will be like this:
SELECT e.*, d.eventDate, t.eventHour
FROM event e
JOIN eventDate d
ON e.id = d.id
JOIN eventTime t
ON e.id = t.id
ORDER BY TIMESTAMP( DATE( d.eventDate ), TIME( t.eventHour ) );
I hope this can help you.
Upvotes: 0
Reputation: 11
This is the best way that worked for me, just convert date to numbers of days whith the function to_days(date) and time to amount of seconds whith the function TIME_TO_SEC(time) , example:
SELECT
nv.destac,
DATE_FORMAT(nv.date_nov , '%d-%m-%Y %T') as date_order,
TO_DAYS(nv.date_nov) as days,
TIME_TO_SEC(TIME(nv.date_nov)) as seconds
FROM
newsviews nv
WHERE
nv.active
ORDER BY
nv.destac DESC, days DESC, seconds DESC ;
Esta es la mejor forma que funcionó para mí, sólo convertir la fecha al número de días con la función TO_DAYS (fecha) y el tiempo a la cantidad de segundos con la funcion TIME_TO_SEC (tiempo)
SELECT
nv.destacar,
DATE_FORMAT(nv.fecha_nov , '%d-%m-%Y %T') as fecha_orden,
TO_DAYS(nv.fecha_nov) as dias,
TIME_TO_SEC(TIME(nv.fecha_nov)) as segundos
FROM
novedades nv
WHERE
nv.activa
ORDER BY
nv.destacar DESC, dias DESC, segundos DESC ;
Upvotes: 1
Reputation: 53
Order it by the id in descending order.
<?php $sql = "SELECT * FROM posts ORDER BY id DESC?>
This should work
Upvotes: 1
Reputation: 1527
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
Upvotes: 14
Reputation: 1
Firstly, it is bad practice to save date and time in different fields.
Anyways, assuming you are saving date/time in the proper format. (i.e: Date format is yyyy-mm-dd and Time is hh:ss)
You first need to concatenate the date and time to get a datetime value. Technically speaking you can do a ORDER BY ON a Datetime field, but it is not good practice again. In our case when we run a CONCAT, so this is converted to a string so the resultset will be wrong. So you need to convert it to a UNIX TIMESTAMP to do a ordering.
Its good practice to run a UNIX_TIMESTAMP on a datetime field before a ORDER by clause.
You can use the following query.
SELECT column1,column2, UNIX_TIMESTAMP(CONCAT(event_date
,' ',event_time
)) as unixtimestamp
from table_name
Order by unixtimestamp desc;
Note:(There is a space between event_date and event_time in the CONCAT function. It is not showing up correctly here.)
Upvotes: 0
Reputation: 10346
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')
Upvotes: 1
Reputation: 881183
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
.
Upvotes: 31
Reputation: 375484
Does
select * from my_table order by eventDate, eventHour
not work?
Upvotes: 3
Reputation: 41823
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)
Upvotes: 4