There are 3 tables: buildings, rooms, reservations.
1 building = n rooms
1 room = n reservations
TABLE BUILDINGS - ID(int), name(varchar)
TABLE ROOMS - ID(int), building_id(int)
TABLE RESERVATIONS - ID(int), room_id(int), date_start(datetime), date_end(datetime)
Buildings table example
<pre>
ID name
1 Building A
2 Building B
3 Building C
</pre>
Rooms table example
<pre>
ID building_id
1 1
2 1
3 2
4 3
</pre>
Reservations table example
<pre>
ID room_id date_start date_end
1 1 2014-08-09 14:00:00 2014-08-09 14:30:00
2 1 2014-08-09 14:30:00 2014-08-09 15:30:00
3 3 2014-08-09 16:30:00 2014-08-09 17:30:00
4 2 2014-08-09 16:00:00 2014-08-09 17:00:00
5 3 2014-08-09 16:00:00 2014-08-09 16:30:00
</pre>
Question
How to filter all buildings which have atleast 1 room available at the specific date and time?
For example we want to filter all buildings which have atleast 1 available room at 2014-08-09 16:00:00 until 2014-08-09 17:00:00. In this case it will show that Building A AND Building C are available. Building A has 1 free room and Building B also has 1 free room, because there is no reservation on that room.
Can someone help me out on this one? I just can not figure it out with a single MySQL query to get all buildings. Thank you.