I'm currently working on a hotel booking service and I want to show all free rooms for a specific roomtype and hotel during a specific time period. I've found what seemed to be a working query until I noticed that: if a room does not have any reservations at all it doesn't work.
SELECT DISTINCT r.id,
r.num,
r.NAME,
h.NAME,
h.city,
h.country
FROM room r
JOIN hotel h
ON r.hotel_id = h.id
JOIN room_type rt
ON r.room_type_id = rt.id
WHERE r.id NOT IN (SELECT room_id
FROM reservation
WHERE ( '$from' BETWEEN start_date AND end_date )
OR ( start_date BETWEEN '$from' AND '$to' )
OR '$from' = start_date)
AND $roomtype = rt.id
AND $hotel = h.id
This query will be used in a PHP script where "$from" is the user's chosen start of reservation date and $to is end of reservation etc.
Is there a way to make this query work even if a room has no reservations? Any help is greatly appreciated!