简体   繁体   中英

Room Availability check SQLite Query

I am a newbie but I am trying to master the art of Web Development and failing miserabely in my learning Process because of an SQLite Query.

This is my DB Design:

在此处输入图片说明

I want to look for available rooms but my queries just don't give me the correct results!!!

I populated the SQLite DB manually with fake bookings and then ran various Queries for the exact same dates already booked but the booked room keeps showing up as available because it IS indeed available before and after the query date! But the Query Dates are the important dates! That is why the booked room should not show up at all!! But i was unable to come up with the correct query!

My Fake Booking:

A Single Room with the Room Number 101 booked from 2016-05-10 to 2016-05-13

This is my "best" Query so far:

SELECT * FROM Rooms NATURAL LEFT JOIN Booking WHERE RoomType='SR' AND
Checkout IS NULL OR Arrival <= '2016-05-10' AND Checkout <= '2016-05-10'
AND RoomType='SR' OR Arrival >= '2016-05-13' AND Checkout >= '2016-05-13'
AND RoomType='SR' OR Arrival <> '2016-05-10' AND Checkout <> '2016-05-13'
AND RoomType='SR';

SR is the RoomType (Single Room)

Room Number 101 needs to vanish entirely from the Resultset when i run the Query! But 101 keeps showing up!

The Arrival and Checkout Fields are of type DATE in the SQLite DB.

Can You guys please help me??

And also, am i doing the PRAGMA Foreign Keys thing correctly in PHP? CustomerID and RoomNumber in the Booking table are Foreign Keys.

<?php
$db = new PDO('sqlite:gshotel.db');
$db->exec('PRAGMA foreign_keys = ON;');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
prepare...
execute...
?>

Thank You very much

Make sure you check the 4 cases you have plus make sure RoomType = 'SR' for all 4 of them. Just simplifying your query and making sure grouping is correct for the conditional.

SELECT * FROM Rooms 
NATURAL LEFT JOIN Booking 
WHERE Checkout IS NULL 
OR (Arrival <= '2016-05-10' AND Checkout <= '2016-05-10')
OR (Arrival >= '2016-05-13' AND Checkout >= '2016-05-13')
OR (Arrival <> '2016-05-10' AND Checkout <> '2016-05-13')
AND RoomType='SR';

Ok, It took me the entire Day but I finally managed to crack it!!

SELECT * FROM Rooms NATURAL LEFT JOIN Booking WHERE
   (
      Checkout IS NULL 
      AND RoomType='SR'
   ) 
   OR (
      (
         '2016-05-10' NOT BETWEEN Arrival and Checkout
      ) 
      AND (
         '2016-05-13' NOT BETWEEN Arrival and Checkout
      ) 
      AND (
         Arrival NOT BETWEEN '2016-05-10' and '2016-05-13'
      ) 
      AND (
         Checkout NOT BETWEEN '2016-05-10' and '2016-05-13'
      ) 
      AND (
         RoomType='SR'
      )   
      AND (
         RoomNumber NOT IN (
            SELECT RoomNumber from Rooms NATURAL LEFT JOIN Booking WHERE
               RoomType = 'SR' 
               AND (
                  Arrival BETWEEN '2016-05-10' and '2016-05-13'
                  OR Checkout BETWEEN '2016-05-10' and '2016-05-13'
                  OR '2016-05-10' BETWEEN Arrival and Checkout
                  OR '2016-05-13' BETWEEN Arrival and Checkout
                  )
         )
      )
   );

I am pretty sure this Query can be further simplified but for now, i am happy that it works! I tested it extensively!

this is an old post but I recently found it and since I took some ideas from this post I'd like to share the solution I came across and works just jine.

SELECT RoomNumber From Rooms WHERE RoomNumber NOT IN
        (SELECT RoomNumber FROM Booking WHERE (('2016-05-10' BETWEEN 
        Arrival AND CheckOut) OR ('2016-05-13' BETWEEN Arrival AND 
        CheckOut) OR (CheckIn BETWEEN '2016-05-10' AND '2016-05-13') OR 
        (CheckOut BETWEEN '2016-05-10' AND '2016-05-13'))) AND 
        Roomtype = "SR"

This is another query using left join as stablished at the begining but it's practical this way, at least not from my perspective.

SELECT * From Rooms LEFT JOIN Booking ON Rooms.RoomNumber = 
        Booking.RoomNumber WHERE Rooms.NumeroHAB NOT IN
        (SELECT Booking.RoomNumber FROM Booking WHERE (('2016-05-10' BETWEEN 
        Arrival AND CheckOut) OR ('2016-05-13' BETWEEN Arrival AND 
        CheckOut) OR (Arrival BETWEEN '2016-05-10' AND '2016-05-13') OR 
        (CheckOut BETWEEN '2016-05-10' AND '2016-05-13'))) AND 
        RoomType = "SR" ORDER BY Rooms.RoomNumber ASC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM