sql >> Base de Datos >  >> RDS >> Mysql

MySQL:¿Es posible crear una consulta que evalúe datos de diferentes registros?

Puede dividir en 2 filas cada fila que contiene Endtime del día siguiente:

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

Vea la demostración .