I am working on the booking / reservation project and faced some interesting issue.
The point of the project is to determine if there is any time slot available for reservation within the given period of time (12:00 - 15:00).
Also, it supports the N amount of rooms / tables allowed to be reserved. (for example, maximum it could be reserved up to 30 rooms)
Currently, data is stored in the mysql db, with the structure:
id, start_time, end_time and some another payload
Firstly, I was sure that I can do a simple sql select do determine overlaps and count the amount of rows returned
sql-query looks like this:
SELECT * FROM reservations WHERE ((start_time < '${end_time}' AND start_time >= '${start_time}')
OR (start_time <= '${start_time}' AND end_time >= '${end_time}') OR (end_time > '${start_time}')
${start_time} - time recieved from the front-end
start_time - column name in the database table.
But eventually I end up with the huge bug. Timeline is horizontal, each rectangle represents one of the created reservations. Empty slot means that there is an empty table / room that could be reserved.
But using this select in order to check available table for time range from 16:00 until 19:00 - query returns more than 50 rows and the if > N_amount replies that there is no available table / room for that time available (but that's obviously not true).
example row:id=1, start_time=2022-08-06 12:00:00, end_time=2022-08-06 15:00:00
Every time slot is rounded to **:30 or **:00 minutes
Any ideas on how to change the query / logic in order to achieve the desired behaviour?
