Friday, 6 September 2013

SQL script that automatically detects free appointments in MySQL database

SQL script that automatically detects free appointments in MySQL database

In my application, users are able to check if a specific appointment is
free. So they choose a specific date and time and specify the length of
the appointment.
An SQL script checks if there is already an appointment at the specific
time. What I want to do now is to offer further appointment options to the
user.
Example: User selects 2013-08-24 18:00:00 as start date and 2013-08-24
18:30:00 as end date. The appointment is already taken but options have to
be offered:
offered appointment can be at the same day
offered appointment can be some hours before or after the prefered
appointment
offered appointment can also be the same date in one week
This is my table (at least the important part of it):
CREATE TABLE `ios_appointments` (
`appointmentId` int(11) NOT NULL AUTO_INCREMENT,
`start` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`end` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
...
)
Example data:
| appointmentId | start | end
=========================================================
| 1 | 2013-08-24 18:00:00 | 2013-08-24 18:30:00
| 2 | 2013-08-24 15:00:00 | 2013-08-24 16:00:00
| 3 | 2013-08-24 09:00:00 | 2013-08-24 12:00:00
If the specified length of the appointment is 2 hours, the proposed
appointments should be something like:
| start | end
=========================================================
| 2013-08-24 12:00:00 | 2013-08-24 14:00:00
| 2013-08-24 13:00:00 | 2013-08-24 15:00:00
How can I accomplish some kind of "smart" sql script?

No comments:

Post a Comment