Best unofficial Apache Server developers community |
|
I'm currently trying to optimize an database by combining queries. But I keep hitting dead ends while optimizing an room availability query. I have a room availability table where each records states the available number of rooms per date. It's formatted like so:
The trouble is getting a list of rooms that are available for EACH of the provided days. When I use IN() like so:
If the 14th has availability 0 it still gives me the other 2 dates. But I only want that room_id when it is available on ALL three dates. Please tell me there is a way to do this in MySQL other than querying each date/room/availability combination separately (that is what is done now :-( ) I tried all sorts of combinations, tried to use room_availability_date = ALL (...), tried some dirty repeating subqueries but to no avail. Thank you in advance for any thoughts!
posted via StackOverflow
|
|
 
|
You would need to construct a query to group on the room ID and then check that there is availability on each date, which can be done using the having clause. Leaving the where clause predicate in for
|
|
 
|
You can group by room ID, generate a list of dates available, and then see if all the dates you need are included. This will give you a list of dates each room is available:
Then we can add a having clause to get the rooms that are available on all of the dates we need:
This should work. Test it for me will ya? :) |
|
 
|
I think I can improve on a'r's answer:
Edit: This of course assumes that there is only one table entry per room per day. Is this a valid assumption? |