I’m creating a room suggestion feature for a room reservation system and I’m trying to figure out how to attack this query. So far I’ve got it excluding what rooms are reserved and in conflict with the desired room. Here is my query:
"SELECT * FROM room
WHERE roomcode NOT IN
(SELECT room FROM event
INNER JOIN room ON event.room = room.roomcode
WHERE (
(begintime <= $start AND endtime >= end)
OR (begintime >= $start AND endtime <= $end)
OR (begintime <= $start AND endtime <= $end AND endtime >= $start)
OR (begintime >= $start AND endtime >= $end AND begintime <= $end)
)
AND room = '$room'
OR room IN ($rooms)
)
AND capacity >= $capacity AND min_capacity <= $capacity";
I’m sorry if that’s a mess.
The information I’m unable to get to right now is a list of rooms that become inactive when a particular room is reserved. For example, a room (n103abc) has three partitions. The whole room can be reserved as one, causing the three parts and their other combinations (n103a, n103b, n103c, n103ab, n103bc) to become inactive. Each room has a column in the database with the rooms it sets inactive formatted as a list separated only by a comma, no spaces.
Basically what’s going on is I’m trying to select rooms that don’t fall in any of the begintime/endtime conditions, aren’t the room reserved in those that fall into the begintime/endtime conditions, and aren’t inactive.
Any suggestions? I know that was a pain in the butt, but I hope someone can show me the light here. I’ve been staring at this for the last 5 hours with no progress. Thanks.
The sentence…
…means your table is not even in the first normal form.
So, you’d probably want to model this hierarchy in a more relational way – for example by introducing a
room.parent_roomcodefield that is a FOREIGN KEY toward theroom.roomcode. If theparent_roomcodeis NULL, this is the “parent” and if it’s non-NULL then this is the “child” (i.e. becomes inactive when parent becomes reserved). Let’s also assume this hierarchy is single-level only (i.e. there are no “grandchildren”).You could then query (roughly) like this:
In plain English, select rooms such that:
Details:
$end <= begintime OR endtime <= $startmeans the event is completely before or completely after the given interval (this works under assumption that $start <= $end and begintime <= endtime). Applying NOT gives an event that is either partially or completely in the interval.events.room = room.parent_roomcodegives events that are connected to parent, meaning the child room is inactive. Since we are dealing with a single-level hierarchy, there is no need for a recursive query.