hi all
I'm a relative newbie to mysql and sql in general. I'm using Mysql 5 to build an appointment booking system. Here's the schema for the table in question:
Code:
CREATE TABLE `bookings` (
`id` int(11) NOT NULL auto_increment,
`emp_id` int(11) default NULL,
`starts_at` datetime default NULL,
`ends_at` datetime default NULL,
PRIMARY KEY (`id`)
)
I want to run a select query that looks for gaps between the value for ends_at on one row and the value for starts_at on the next row. After days of trawling forums and web sites here's what I've come up with:
Code:
SELECT *, TIMEDIFF(b.starts_at, a.ends_at) AS 'length'
FROM bookings AS a
JOIN bookings AS b ON a.emp_id = b.emp_id
AND a.ends_at < b.ends_at
GROUP BY a.ends_at
don't ask why I'm using that query - I'm in way over my head and that's the sad result of a lot of trial and error. Ideally, I'd like to set conditions specifying the earliest start time and required appointment duration, but right now I'd settle just for a full list of available appointments.
I'm not even sure if this is possible in mysql. I've seen solutions for other DBs but they go on about pivot table and analytic functions and all sorts!
any help
gratefully received
rob