Jump to content

Double sub query

- - - - -

  • Please log in to reply
3 replies to this topic

#1
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
Hello,
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?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Start with the ClientRoom table: How can you get ALL records that are in the future? Once you have that, it's just a matter of creating the joins to the other two tables to get them all.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
I look at dateIn_ClientRoom if it's bigger (>) than CURRENT_TIMESTAMP
But this is only if this room isn't new, if it's the case, ClientRoom for this room dosen't exist

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
When you're building SQL statements like this, I like to have a good SQL client (such as SQuirreL) and build it slowly. Rooms that are reserved in the future will definitely have that record. Now you start creating the necessary joins to get whatever extra data you need from the other tables.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users