I’m dealing with (for me) huge SQL problem. I have table called “timeshifts” which as 4 columns:
id, name, from and to. Last two type is TIME.
Now: I have three timeshifts:
06:00 – 14:00
14:00 – 22:00
22:00 – 06:00
Now, I have to find correct shift for current time (which works like a charm), but I can’t find a proper records that belong to current shift. IE. table called user_times contains fields with separate date and time values. According to current timeshift I have to find records that belongs to the timeshift. There is no problem for first two shifts, but third one passes midnight and that is my problem.
Here is the query that finds proper shift, event through midnight:
SELECT
FLOOR(SUM(time_sum) / 60) AS th,
MOD(SUM(time_sum), 60) AS tm,
t.*
FROM
ur_user_times ut,
ur_timeshifts t
WHERE CURTIME() >= IF (
t.timeshift_to < t.timeshift_from,
0,
ADDTIME(t.timeshift_from, "00:15")
)
AND CURTIME() <= ADDTIME(
IF (
t.timeshift_to < t.timeshift_from,
ADDTIME(t.timeshift_from, "10:00:00"),
t.timeshift_to
),
"00:15"
)
AND ut.`date` BETWEEN CURDATE() - INTERVAL 1 DAY AND CURDATE()
AND ut.`start` BETWEEN ADDTIME(t.`timeshift_from`, "-10:00:00") AND ADDTIME(t.`timeshift_to`, "-10:00:00")
AND ut.user_id = 40
ut.start is time value that interests us.
Try this: