I need to be able to check whether a date range given by the user is available. I have a table called booked
in my database which holds a start date, end date, and the amount of rooms that have been booked over that period.
Lets say my booked
table looks like this:
room_id | start | end | amount_of_rooms_booked
1 | 2012-12-12 | 2012-12-15 | 2
1 | 2012-12-13 | 2012-12-16 | 3
1 | 2012-12-18 | 2012-12-19 | 1
If there are 8 rooms available in total for room_id
1, and the user asks for this room between 2012-12-10
and 2012-12-20
, is it possible to do a query that checks whether the SUM
of rooms that have been booked for each day does not exceed 8.
The other way I could do this would be to store each day individually, and have the booked
table like this instead:
room_id | start | amount_of_rooms_booked
1 | 2012-12-12 | 2
1 | 2012-12-13 | 5
1 | 2012-12-14 | 5
1 | 2012-12-15 | 3
1 | 2012-12-18 | 1
That way would use more space in the database but would probably be easier. Which should I do? And if the first idea how would I do the query? Are there any other better ways to achieve what I'm trying to do?