i am on a reservation system for guided tours. And before anyone complains about this question to be a dublicate of this one here: Help with SQL query to find next available date for a reservation system i want to point out that I don't want to use a calender table.
Some basic information:
Reservation table structur:
CREATE TABLE IF NOT EXISTS `reservations` ( `id` int(11) NOT NULL AUTO_INCREMENT, `guider_id` int(11) NOT NULL, `date` datetime NOT NULL, `duration` int(11) NOT NULL, PRIMARY KEY (`id`), ) ;
userStartDateTime = the start date and time the user choose in the reservation system userEndDateTime = the start date and time the user choose in the reservation system + the duration
Minimum duration for a tour is 15 minutes.
Here's the deal: Customers come to the reservation system. They choose a date and a time, a duration and a range in case the choosen date/time is not available anymore.
We have different guides but it's always the same tour only differed by the duration they take and the places to see (which does not matter at this point). So the reservations can overleap themselves up to a certain level. Every guider is available at everytime of the day, 24/7. But there is a buffer of 5 minutes after every tour to avoid delays.
I generate the end date by adding the duration of a tour to the start time. I searching in the reservations table for all taken guides with a BETWEEN where clause like this:
SELECT guider_id FROM reservations as r
WHERE
(((r.startDateTime - interval 5 minute) BETWEEN userStartDateTime AND userEndDateTime)) OR
((r.startDateTime + interval r.duration minute + interval 5 minute) BETWEEN userStartDateTime AND userEndDateTime))
Afterwards I use "NOT IN" on the guiders table to find available guiders (like mentioned every guider is available at every time of day). If i find one: yeah, reservation successfull. If not...I need to find a date/time where one is available.
That is where i am stucked. I do not want to create a table where every available date time combination is stored because that is hell of a memory waste and means hell of a lot work and performance. Because every time of the day could be choosen. Which means, with every reservation i need to re-arrange the whole table for that day. That table would hold already 1440 entries for a day and for a year (assuming 360 days) 518400. To fill that table for the upcoming ten years...you can calculate yourself. That's not even worth thinking about it.
So what i need to do now is: i need to find a new date/time within the range from the userStartDateTime. First forward (preferred) and if no free guider has been found by that backward from that.
So how may i do this?
I just have no working idea about that. Just adding the range to my calculated end date would not work, 'cause it would catch to much guiders which have a tour starting and finishing within it.
Greetings func0der