【问题标题】:Check availability and list available rooms [closed]检查可用性并列出可用房间[关闭]
【发布时间】:2014-07-10 08:45:43
【问题描述】:

这些天,我正在为我的酒店客房预订系统学位项目努力工作。
现在我进入了检查可用性阶段。
这是我的表结构


预订

`ID` int(11) 
`roomID` int(11)
`startDate` date 
`endDate` date


客房桌

`roomID`    int(11)              
`name`  varchar(255)                     
`facilities`  mediumtext

假设有 5 个房间。 roomID 是 01,02,03,04 和 05。 在这些房间中,01 已被预订。入住日期为 2013 年 5 月 11 日,退房日期为 2013 年 11 月 10 日。

还有 roomID 04,于 2013 年 8 月 11 日至 2013 年 9 月 11 日预订


如果用户在 2013 年 4 月 11 日至 2013 年 11 月 10 日期间检查可用性,则应仅显示房间 ID 02,03 和 05。

如何构建系统来显示结果?

有人可以帮我构建 sql 查询来检查可用性吗?

【问题讨论】:

  • 第 1 步。学习 SQL。第 2 步。尝试!
  • 我还是 SQL 的初学者。这就是我问你的原因
  • Google 有大量的 SQL 教程供你参考,StackOverflow 是针对特定编程问题的(意味着你必须进行编码尝试)

标签: php mysql


【解决方案1】:

这里的逻辑实际上非常简单,您不希望开始和结束在您的句点内,或者它们位于您正在寻找的范围内任何点的相对两侧。

SELECT
    R.roomID,
    R.name,
    R.facilities
FROM
    -- Use the table rooms, but name it R for shorthand.
    Rooms R

    -- Left joins return null if there's no match, which we use in the where clause to identify these rows
    LEFT JOIN Bookings B
    -- B is bookings, R is rooms, they're aliased to make things easier
    ON B.RoomId = R.RoomId
        AND
        (
            -- As I said at the top of this answer, we want to exclude anything that matches any of 3 conditions:
            -- The start date is between the date's the room is already booked for
            @SearchCheckingStart BETWEEN B.startDate AND B.endDate
            OR
            -- The end date is in the period where the room is already booked
            @SearchCheckingEnd BETWEEN B.startDate AND B.endDate
            OR
            -- Or our date range lies between the period we're booking for
            B.startDate BETWEEN @SearchCheckingStart AND @SearchCheckingEnd
        )

    WHERE -- We're only interested in results where MySQL couldn't match the results earlier, this gives us the rooms that are free.
        B.RoomId IS NULL

【讨论】:

  • 从 cmets 可以看出,OP 几乎没有使用 SQL 的经验。为了使它成为一个好的答案,您必须解释此查询的工作原理,包括连接、之间的连接以及可能的别名。 (当你回答不好的问题时会发生这种情况)
  • 亲爱的 scragar 非常感谢您的宝贵回复,请您简要解释一下,最后 10 行。 PLZ PLZ PLZ
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-23
  • 2016-08-12
  • 2019-07-16
  • 2012-06-03
  • 1970-01-01
  • 1970-01-01
  • 2021-03-18
相关资源
最近更新 更多