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

MySQL / PHP - Trova le fasce orarie disponibili

SELECT a.end AS free_after
FROM bookings a
WHERE NOT EXISTS (
  SELECT 1
  FROM bookings b
  WHERE b.start BETWEEN a.end AND a.end + INTERVAL your_duration HOURS
)
AND a.end BETWEEN start_of_search_window AND end_of_search_window;

devi solo fornire i valori per your_duration (numero intero), start_of_search_window (data e ora) e end_of_search_window (data ora).

E se vuoi campanelli e fischietti....

SELECT free_from, free_until
FROM (
  SELECT a.end AS free_from,
  (SELECT MIN(c.start)
   FROM bookings c
   WHERE c.start > a.end) as free_until
  FROM bookings a
  WHERE NOT EXISTS (
    SELECT 1
    FROM bookings b
    WHERE b.start BETWEEN a.end AND a.end + INTERVAL your_duration HOUR
  )
  AND a.end BETWEEN start_of_search_window AND end_of_search_window
)
ORDER BY free_until-free_from
LIMIT 0,3;

Ti darà i tre slot disponibili più corti (cioè il più vicino alla dimensione target) all'interno della finestra.