You can split to 2 rows each row that contains Endtime
of the next day:
WITH cte AS (
SELECT *, Weekday day, Starttime time1, Endtime time2 FROM user_time
WHERE Starttime < Endtime
UNION ALL
SELECT *, Weekday, Starttime, '23:59' FROM user_time
WHERE Starttime > Endtime
UNION ALL
SELECT *,
DATE_FORMAT((STR_TO_DATE(CONCAT(LEFT(CURRENT_DATE, 8), Weekday), '%X-%V-%W') + INTERVAL 1 DAY), '%a'),
'00:00', Endtime
FROM user_time
WHERE Starttime > Endtime
)
SELECT No, Restaurant, Weekday, Starttime, Endtime
FROM cte
WHERE day = DATE_FORMAT(CURRENT_DATE, '%a')
AND NOW() BETWEEN CONCAT(CURRENT_DATE, ' ', time1, ':00') AND CONCAT(CURRENT_DATE, ' ', time2, ':00')
See the demo.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…