2

I have a mysql table with 3 columns "Event" in text format, "Date" in format YYYY-MM-DD and "Hour" in format HH:MM. For example:

Event1  2013-08-20  18:30
Event2  2013-08-20  19:30
Event3  2013-08-20  20:00
Event4  2013-08-20  00:00
Event5  2013-08-20  02:30
Event6  2013-08-20  14:50
Event7  2013-08-20  00:30

The database contains various days and various hours into a day. I have to order all the events first in days and later in hours, but the order of hours have to be like this example:

Event6  2013-08-20  14:50
Event1  2013-08-20  18:30
Event2  2013-08-20  19:30
Event3  2013-08-20  20:00
Event4  2013-08-20  00:00
Event7  2013-08-20  00:30
Event5  2013-08-20  02:30

The hours 00:00, 01:00, 02:00... are normally at the beginning, but I need to change the order. The hours 00:00, 01:00, 02:00... should appear at the end, like you can see in the last example.

My sentence is:

SELECT * FROM Events ORDER BY Date, Hour

But this sentence is not the appropiate for me because it get me back this values:

Event4  2013-08-20  00:00
Event7  2013-08-20  00:30
Event5  2013-08-20  02:30
Event6  2013-08-20  14:50
Event1  2013-08-20  18:30
Event2  2013-08-20  19:30
Event3  2013-08-20  20:00

Which sentence in MySql or PHP need I to show the results like I want?

Thanks for all ;)

2 Answers 2

1

You can use MySQL function TIME_TO_SEC() and IF statement in ORDER BY.

Let's say everything bellow 05:00 should be at the end :

SELECT
   *
FROM
   `Events`
ORDER BY
  `Date` ASC,
  IF(TIME_TO_SEC(`Hour`) < TIME_TO_SEC('05:00'), 1, 0) ASC,
  `Hour` ASC

Example of SQLFIDDLE.

Sign up to request clarification or add additional context in comments.

6 Comments

I have put this records into database table, tested it, and it works. Can you paste sql dump for table and records so I can test it on your data? +I have updated the answer with sqlfiddle example
I understood that you have DATE field type for date field, and TIME field type for hour? But still, your data is working for me (but DATE sorting will not work)... sqlfiddle.com/#!2/77abc/1 Can you put your not-working example on sqlfiddle.com ?
Because you are sorting by string 'Hora', and not field `Hora`. Your fixed sql statement > sqlfiddle.com/#!2/c201a/6 . And change Hora field type to TIME instead of VARCHAR...
Yeeeessss!!! You are the best! I work with Hora (varchar) and your sentence IF(TIME_TO_SEC(Hour) < TIME_TO_SEC('05:00'), 1, 0) ASC, Hour ASC. I was wrong with `'. 1000 Thanks!
with de type TIME in field Hora... the result is January, 01 1970 23:00:00+0000.... 1970??
|
0

Have you tried

SELECT * FROM Events ORDER BY Date, time(Hour)

1 Comment

this solution not work, the result is the same. The column of Hour is in String...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.