I try to create an application for the iPhone where you can set appointments. Everything is saved into a MySQL database and I currently get the data through JSON into my app. This is a workflow:
- User1 defines when he is working. E.g. 8am – 4pm.
- User2 wants to have an appointment with user1, e.g. 8am-9am.
The script should be able to do this:
- the appointment is within the user’s work hours; and
- it does not clash with an existing appointment, which can happen in three possible ways:
- the clashing appointment starts during the new appointment; and/or
- the clashing appointment ends during the new appointment; or
- the clashing appointment starts before and ends after the new appointment.
These are the important tables:
// new row should be added here when the conditions above are met
create table ios_appointment (
appointmentid int not null auto_increment,
start timestamp,
end timestamp,
user_id_fk int
)
// a working hour has a n:1 relationshipt to ios_worker
create table ios_workinghours (
workinghoursid int not null auto_increment,
start timestamp,
end timestamp,
worker_id_fk int
)
// employee, has a 1:n relationship to ios_workinghours
create table ios_worker (
workerid int not null auto_increment,
prename varchar(255),
lastname varchar(255),
...
)
The input for the select clause are two timestamps, start and end. These are defined by the user. So the script should check if user 2 is working at that specific time and if there are already appointments.
I currently have something like this, but that uses the user_id to link the tables:
SELECT EXISTS (
SELECT *
FROM ios_appointments a JOIN ios_workhours h USING (user_id)
WHERE user_id = 1
AND h.start <= '08:00:00' AND h.end >= '09:00:00'
AND (
a.start BETWEEN '08:00:00' AND '09:00:00'
OR a.end BETWEEN '08:00:00' AND '09:00:00'
OR (a.start < '08:00:00' AND a.end > '09:00:00')
)
LIMIT 1
)
Every help is appreciated. Thx.
You either need to have your app read in the data and determine if the time is available OR you need to create a view that has the available “time slots” (e.g. every 30 minutes).
Here’s how I would do it:
This will leave you with a data set that indicates the available and non-available timeslots.
Hope this helps!