MySQL TIME comparisons
-
25-06-2021 - |
Domanda
I need to store a TV guide in MYSQL and I was thinking something like this:
channel_id day start_time end_time
AMC Breaking Bad:) Monday 19:00:00 20:30:00
Other Program Saturday 09:30:00 12:30:00
end_time
and start_time
are TIME
fields and store HOURS.
My problem and questions is:
How can I get from MYSQL what programs are watching NOW()? I don't know how to compare the hours. I done some test, but i get nothing.
Hope you understand my English! Thanks Guys!
Soluzione
I think you are asking how I can get a list of programs that are on right now where NOW is greater than or equal to the start_time and less than the end_time. With those assumptions here is the SQL:
SELECT * FROM tv_guide
WHERE CURTIME() BETWEEN start_time AND end_time;
EDIT: As @Corbin notes, you will need to check the day.
Altri suggerimenti
SELECT * FROM shows WHERE CURRENT_TIME() BETWEEN start_time AND end_time;
You'll probably also want to check the day as well, but that's just another where clause (AND day = 'Friday'
).