简体   繁体   English

房间可用性检查 SQLite 查询

[英]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.我是一个新手,但我正试图掌握 Web 开发的艺术,但由于 SQLite 查询而在我的学习过程中惨遭失败。

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!我用虚假预订手动填充 SQLite 数据库,然后针对已预订的完全相同的日期运行各种查询,但预订的房间一直显示为可用,因为它确实在查询日期之前和之后可用! 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 2016年5月10日至2016年5月13日预订101号房的单人房

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) SR是 RoomType(单人间)

Room Number 101 needs to vanish entirely from the Resultset when i run the Query!当我运行查询时,房间号 101 需要从结果集中完全消失! But 101 keeps showing up!但是101一直出现!

The Arrival and Checkout Fields are of type DATE in the SQLite DB. ArrivalCheckout字段在 SQLite 数据库中属于DATE类型。

Can You guys please help me??你们能帮帮我吗??

And also, am i doing the PRAGMA Foreign Keys thing correctly in PHP?而且,我是否在 PHP 中正确地执行了 PRAGMA 外键操作? CustomerID and RoomNumber in the Booking table are Foreign Keys. Booking 表中的 CustomerID 和 RoomNumber 是外键。

<?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.确保您检查了您拥有的 4 个案例,并确保所有 4 个案例的 RoomType = 'SR'。 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.这是一篇旧帖子,但我最近找到了它,因为我从这篇文章中汲取了一些想法,所以我想分享我遇到的解决方案,并且仅适用于 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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