This SQL statement selects the room numbers for all occupied rooms at a hotel and works well. However i wish to show all the absent rooms instead. As I am sure you know I can't just "NOT" the statement as it would select the wrong date. If anyone has a solution I would appreciate an explanation
NOTE: Date stored in the format 2016-11-25 I convert it for comparison purposes.
SELECT room_no FROM booking WHERE (booking_date + 0) < ('20161125') AND (booking_date + nights - 1) > ('20161125') ORDER BY room_no
SELECT room_no
FROM booking
NOT IN
(
SELECT room_no
FROM booking
WHERE (booking_date + 0) < ('20161125') AND (booking_date + nights - 1) > ('20161125')
)
ORDER BY room_no
On second thought, it might not work because the query is referencing the 'booking' table which, by deduction, might not contain all available rooms of the hotel, only the ones that are/were booked. You probably should need a table room with a record for each room of the hotel or record the maximum number of room in the hotel. I'm a mistaken ?
SELECT id FROM room WHERE id NOT IN ( SELECT room_no FROM booking WHERE (booking_date + 0) < ('20161125') AND (booking_date + nights - 1) > ('20161125') ) ORDER BY id