I have this database (I translate the database in english to make more sens)
CREATE TABLE `Room` ( `id_Room` int(11) NOT NULL AUTO_INCREMENT, `id_Departement` int(11) NOT NULL, `name_Room` varchar(20) COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (`id_Room`), KEY `id_Departement` (`id_Departement`), CONSTRAINT `Room_ibfk_1` FOREIGN KEY (`id_Departement`) REFERENCES `Departement` (`id_Departement`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci CREATE TABLE `Client` ( `id_Client` int(11) NOT NULL AUTO_INCREMENT, `name_Client` varchar(200) COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (`id_Client`) ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci CREATE TABLE `ClientRoom` ( `id_ClientRoom` int(11) NOT NULL AUTO_INCREMENT, `id_Room` int(11) NOT NULL, `id_Client` int(11) NOT NULL, `dateIn_ClientRoom` date NOT NULL, `dateOut_ClientRoom` date DEFAULT NULL, PRIMARY KEY (`id_ClientRoom`), KEY `id_Client` (`id_Client`), KEY `id_Room` (`id_Room`), CONSTRAINT `ClientRoom_ibfk_2` FOREIGN KEY (`id_Room`) REFERENCES `Room` (`id_Room`), CONSTRAINT `ClientRoom_ibfk_1` FOREIGN KEY (`id_Client`) REFERENCES `Client` (`id_Client`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
dateIn_ClientRoom represent the date where the client check in for the room
and dateOut_ClientRoom represent the date when the client leave
if dateOut_ClientRoom is null, is because the client is still in the room
And if dateOut_ClientRoom is in the future is because the client is once again still in the room.
To find what room are free I use this command:
select c.id_Room from Room c where c.id_Room not in (select rc.id_Room from ClientRoom rc where (rc.dateOut_ClientRoom is null or rc.dateOut_ClientRoom > CURRENT_TIMESTAMP) and rc.dateIn_ClientRoom < CURRENT_TIMESTAMP)
Now I wish to find what room are free right now, but are reserved in the future (have a dateIn_ClientRoom in the future)
I know I could use the commande above inside a subquery with a "not in", but I don't like the idea of having 3 query (1 main, 1 sub query, and 1 sub sub query) for a single query.
Do you see a other way to do so?


Sign In
Create Account


Back to top









