I need to do a very complex search
Context: I have 3 tables: rooms, reservation, and room_reservation. The first one have all info of the rooms in a hotel, the second have info of all reservation and the third is because a reservation have multiple rooms.
I need know what rooms are available in a expecific date, I really close, I need that if at least one data does not match the condition of the search then the related room is not shown.
This is my query:
SELECT DISTINCT hab.* FROM habitacion hab
LEFT OUTER JOIN habitacion_reserva habr ON habr.id_habitacion = hab.id
LEFT OUTER JOIN reserva res ON res.id = habr.id_reserva
WHERE hab.tipo = 1
AND (((( '2018-06-10' not between res.fecha_ingreso and res.fecha_salida) AND ( '2018-06-17' not between res.fecha_ingreso and res.fecha_salida))
OR (res.fecha_ingreso is null OR res.fecha_salida is null)) OR ((( '2018-06-09' between res.fecha_ingreso and res.fecha_salida) OR ( '2018-06-17' between res.fecha_ingreso and res.fecha_salida)) AND res.estado = 4))
In theory the query works but when I have many reservation, the query simply return all rooms.