I have 3 tables, that i want to join, one is room the second is reservation and the third is is reservationroom
Here are my tables structure:
CREATE TABLE `room` (
`roomID` int(11) NOT NULL AUTO_INCREMENT,
`hotelID` int(11) NOT NULL,
`roomtypeID` int(11) NOT NULL,
`roomNumber` int(11) NOT NULL,
`roomName` varchar(255) NOT NULL,
`roomName_en` varchar(255) NOT NULL,
`roomDescription` text,
`roomDescription_en` text,
`roomSorder` int(11) NOT NULL,
`roomVisible` tinyint(4) NOT NULL,
PRIMARY KEY (`roomID`)
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8;
CREATE TABLE `reservation` (
`reservationID` int(11) NOT NULL AUTO_INCREMENT,
`customerID` int(11) NOT NULL,
`hotelID` int(11) NOT NULL,
`reservationCreatedOn` datetime NOT NULL,
`reservationCreatedFromIp` varchar(255) CHARACTER SET greek NOT NULL,
`reservationNumberOfAdults` tinyint(4) NOT NULL,
`reservationNumberOfChildrens` tinyint(4) NOT NULL,
`reservationArrivalDate` date NOT NULL,
`reservationDepartureDate` date NOT NULL,
`reservationCustomerComment` text CHARACTER SET greek,
PRIMARY KEY (`reservationID`)
) ENGINE=InnoDB AUTO_INCREMENT=47 DEFAULT CHARSET=utf8;
CREATE TABLE `reservationroom` (
`reservationroomID` int(11) NOT NULL AUTO_INCREMENT,
`reservationID` int(11) NOT NULL,
`hotelID` int(11) NOT NULL,
`roomID` int(11) NOT NULL,
PRIMARY KEY (`reservationroomID`)
) ENGINE=InnoDB AUTO_INCREMENT=47 DEFAULT CHARSET=utf8;
The reason why i have the reservationroom table is simple, one reservation can have more than one room.
What i am trying to do? I would like to check if the room is available for certain period.
Here is my current query (which produce wrong results):
SELECT * FROM reservation re
LEFT JOIN reservationroom rr
ON re.reservationID = rr.reservationID
LEFT JOIN room r
ON rr.roomID = r.roomID
WHERE NOT (re.reservationArrivalDate BETWEEN '2014-07-07' AND '2014-07-10' )
AND NOT (re.reservationDepartureDate BETWEEN '2014-07-07' AND '2014-07-10' )
AND re.hotelID = 14
GROUP BY rr.roomID
Can anyone tell me what i am doing wrong? Hotel with id 14 has only one reservation for this period, and hotel has 6 rooms, so i should get 5 results, and I am getting none (0).
Any help will be deeply appreciated.
Regards, z0reli