Suppose we have this table..
CREATE TABLE `appointments` (
`idappointments` int(11) NOT NULL AUTO_INCREMENT,
`timeStart` time DEFAULT NULL,
`timeEnd` time DEFAULT NULL,
PRIMARY KEY (`idappointments`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8$$
assumption
Suppose that a range between timeStart and timeEnd cant exist again… I mean that if we intersect all the ranges in the table the result would be EMPTY ,0 , null.An appointment cant cooexist with another..
So what i want to do is a time suggestion if the desired time is occupied…
A proposal before and a proposal after the desired time….
So i was wondering instead of writing code to do this if i could write an SQL query to find the nearest empty ranges……
Ex.. timeStart – NEAREST_TO_TIMESTART_TIMEEND >’10 minutes’ whereas 10 minutes the duration
MySQL doesn’t have recursive functionality, so you’re left with using the NUMBERS table trick –
Create a table that only holds incrementing numbers – easy to do using an auto_increment:
Populate the table using:
…for as many values as you need.
Use DATE_ADD to construct a list of dates, increasing the days based on the NUMBERS.id value. Replace “2010-01-01” and “2010-01-02” with your respective start and end dates (but use the same format, YYYY-MM-DD HH:MM:SS) –
LEFT JOIN onto your table of data based on the datetime portion.
This will show you the first available slot:
This will show you availability for the entire day:
Why Numbers, not Dates?
Simple – dates can be generated based on the number, like in the example I provided. It also means using a single table, vs say one per data type.