【问题标题】:Select available rooms in given date range选择给定日期范围内的可用房间
【发布时间】:2014-06-14 06:33:15
【问题描述】:

我正在构建一个酒店预订系统,但在尝试进行查询以检索给定范围内的所有可用房间类型时卡住了。

我有两张桌子RoomsReservationsRooms 桌子存放着酒店的房间。他们的号码 (id) 和他们的类型 (type)。

Table Reservations 保存客户的预订。预订号 (id)、相关房间号 (room_id) 和日期范围(fromto

我试过这个查询:

SELECT room_id as available_room_number, type

FROM roomstesting

LEFT JOIN reservations ON roomstesting.id = reservations.room_id
WHERE reservations.id 
  NOT IN (reservations.e_from <='"2014-03-07 19:00:00' 
           AND reservations.e_to >='2014-03-08 19:00:00')

我试图获取 3 月 7 日至 3 月 8 日范围内的所有可用房间类型。我希望得到 modern room 作为查询的结果。因为modern room id 4 没有与日期范围重叠的预订,并且所有其他 3 个房间都有从 3 月 6 日到 3 月 9 日的预订。但我没有得到我想要的结果。下面是我的数据库的结构(简化)

房间

| id | type         |
|||||||||||||||||||||
|  1 | master suite |
|  2 | master suite |
|  3 | modern room  |
|  4 | modern room  |

预订

| id | room_id | from                | to                  |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|  1 |    1    | 2014-03-05 08:00:00 | 2014-03-09 08:00:00 |
|  2 |    2    | 2014-03-05 08:00:00 | 2014-03-09 08:00:00 |
|  3 |    3    | 2014-03-05 08:00:00 | 2014-03-09 08:00:00 |

预期结果

| available_room_number | type       |
||||||||||||||||||||||||||||||||||||||
|          4            | modern room|

如果有人能告诉我应该如何处理这个问题,那就太完美了。期待您的回复。

【问题讨论】:

    标签: mysql sql


    【解决方案1】:

    试试这个:

    SELECT * FROM Rooms WHERE ID NOT IN(SELECT room_id FROM reservations WHERE '2014-03-07 19:00:00' < e_to AND '2014-03-08 19:00:00' > e_from)
    

    【讨论】:

    • 耶?!这适用于在该范围内开始/结束的所有预订,但不考虑在该范围外开始和结束的预订。我已将内部选择调整为此 SELECT room_id FROM reservations WHERE '2014-03-07 19:00:00' &lt; e_to AND '2014-03-08 19:00:00' &gt; e_from 并且它有效。如果您可以编辑对此的答案,我很乐意接受。
    【解决方案2】:

    为什么不直接反转比较符号

    WHERE reservations.e_from >'2014-03-07 19:00:00' AND reservations.e_to

    【讨论】:

      【解决方案3】:

      4 号房间没有预订,所以JOIN 没有接机。试试这样的:

      SELECT    room_id, type
      FROM      roomstesting
      WHERE     room_id NOT IN (
                   SELECT    reservations.room_id
                   FROM      reservations
                   WHERE     (
                                reservations.e_from <= '2014-03-07 19:00:00' AND
                                reservations.e_to >= '2014-03-08 19:00:00'
                             ) OR (
                                reservations.e_from <= '2014-03-07 19:00:00' AND
                                reservations.e_to < '2014-03-08 19:00:00'
                             ) OR
                             (
                                reservations.e_from > '2014-03-07 19:00:00' AND
                                reservations.e_to >= '2014-03-08 19:00:00'
                             ) OR
                             (
                                reservations.e_from > '2014-03-07 19:00:00' AND
                                reservations.e_to < '2014-03-08 19:00:00'
                             )
                             )
      

      【讨论】:

        【解决方案4】:

        我总是尝试使用 BETWEEN 并且它有效

        select * from roomstesting 
        where room_id not in
        (select room_id from roomstesting where date between begin_date and end_date)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-08-03
          • 2016-06-02
          • 1970-01-01
          • 1970-01-01
          • 2017-06-16
          相关资源
          最近更新 更多