I'm doing a feasibility study for an accommodation booking database. I have two tables as shown below (there are some other fields in the 'rooms' table, which I've omitted here).
In my prototype database I have just 15 rooms. The bookings table contains many entries (about 210, covering January 2013 only), each linking a specific room to a date when it is booked. The partial 'bookings' table looks like this:Code:tbl_rooms ------- room_id property_id room_type ....etc tbl_bookings ------ room_id book_date
I can readily find out which rooms are booked on a certain date with a query like this:Code:room_id book_date 2043.01 01/01/2013 2043.01 03/01/2013 2043.01 06/01/2013 2043.01 07/01/2013 2043.01 14/01/2013 2043.01 20/01/2013 2043.01 21/01/2013 2043.01 22/01/2013 2043.01 23/01/2013 2043.01 24/01/2013 2043.01 26/01/2013 2043.01 29/01/2013 2043.01 30/01/2013 2043.01 31/01/2013 2043.02 01/01/2013 2043.02 03/01/2013 2043.02 07/01/2013 2043.02 19/01/2013 2043.02 21/01/2013
But I haven't yet found a way to determine which rooms are NOT booked, via a single query. Using something likeCode:SELECT r.room_id FROM `rooms` AS r INNER JOIN `bookings` AS b ON r.room_id = b.room_id WHERE b.book_date = '2013-01-14'
selects all the rooms, because they all have at least one booking which is not on that date.Code:WHERE book_date != '2013-01-14'
Having found the booked rooms I could get the free ones by subtraction, but surely there's a better way ?
Eventually I need to find rooms that are free for several consecutive days.
Can anyone help, please ?



Reply With Quote






Bookmarks