Mysql
 sql >> Database >  >> RDS >> Mysql

MySQL:è possibile creare una query che valuti i dati di record diversi?

Puoi dividere in 2 righe ogni riga che contiene Endtime del giorno successivo:

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') 

Vedi la demo .