Here is a simplified schema of my database:
Users
------------------------------------
UserID | Name | Assignable
------------------------------------
5 | John Doe | 1
TimeSlots
-------------------------------------------------------
TimeSlotID | StartTime | EndTime
-------------------------------------------------------
3 | 2011-06-30 15:00:00 | 2011-06-30 16:00:00
Appointments
------------------------------------
TimeSlotID | UserID
------------------------------------
3 | 5
I have Users that can be assigned to TimeSlots that make up Appointments. I need a way to start with a start and end time and query all Users that are able to be assigned (Assignable flag set to 1) and have no time conflicts (Appointments that overlap) with those times.
Older TimeSlots will be in there too so you would only be interested in TimeSlots that are now or in the future.
edit Taking a cue from tandu
I think this would also work, and it has the added performance benefit of no subqueries: