I'm looking for a way to select the row in which the current time is between two set values in the row. I've set up a table with 3 columns, 2 of them hold a timestamp (HH:MM:SS), the other one a string. Is there a way I can get the string corresponding to the current time?
To put it in a more abstract way:
SELECT String FROM TableName WHERE (Current Time) BETWEEN (Lower Limit Time Value) AND (Upper Limit Time Value);
So basically, based on the current time, my script should output the correct string.
How do I go about this?
Thanks!
解决方案
In MySQL, timestamp is quite a confusing word.
If they are lowerlimit and upperlimit are TIME columns from 00:00:00 to 23:59:59:
SELECT String
FROM TableName
WHERE CURTIME() BETWEEN lowerlimit AND upperlimit
OR CURTIME() BETWEEN SUBTIME(upperlimit, '24:00:00') AND lowerlimit
OR SUBTIME(CURTIME(), '24:00:00') BETWEEN SUBTIME(upperlimit, '24:00:00') AND lowerlimit
This will handle midnight transitions correctly.