mysql - 当一次预订中有多个类别时,获取两个日期之间的可用房间

标签 mysql sql laravel eloquent

我的问题是,当一次预订中可以有多种房型时,我不知道在 2 个日期之间获得可用房间时该怎么做。

我有 3 个表:

reservedRoom

    create table reservedRoom (
bookingID int ,
roomID int NOT NULL,
n_person int,
)



bookingID   roomID  n_person
2            1          2
2            2          2
2            3          3
2            4          3
2            5          3

房间

create table room (
roomID int NOT NULL PRIMARY KEY,
descriptions int NOT NULL,
rate int,
category varchar(30),
)
roomID  descriptions    rate    category
1              2        2000    standard
2              2        2000    standard
3              2        2000    standard
4              2        2000    standard
5              2        2000    standard
6              2        2000    standard
7              2        2000    standard
8              2        2000    standard
9              2        2500    quad
10             2        2500    quad
11             2        2500    quad
12             2        2500    quad
13             2        2500    quad
14             2        3000    family
15             2        3000    family
16             2        3000    family
17             2        3000    family
18             2        8000    King
19             2        8000    King
20             2        8000    King

预订

create table bookings (
bookingID int NOT NULL PRIMARY KEY,
clientID int NOT NULL,
checkIndate DATETIME,
checkOutDate DATETIME,
roomsCount int,
numNights int,
bookExpire DATETIME,
)

   bookingID    clientID    checkIndate checkOutDate    roomsCount  numNights   bookExpire
1                 1        2018-02-08   2018-02-09      3             2     2018-02-11 
2                 2        2018-02-08   2018-02-09      5             2     2018-02-11 
3                 3        2018-02-08   2018-02-09      3             2     2018-02-11 
4                 4        2018-02-08   2018-02-09      3             2     2018-02-11 
5                 5        2018-02-08   2018-02-09      3             2     2018-02-11 

我试过这段代码,但我不知道从这里可以做什么。

 $availableRooms = booking::where(function($query) use ($checkInDate_1, $checkOutDate_1)
                {
                  $query->where(function($query) use ($checkInDate_1, $checkOutDate_1){
                      $query->whereDate('bookings.checkOutDate', '>=', $checkInDate_1);
                      $query->whereDate('bookings.checkOutDate', '<=', $checkOutDate_1);
                  });
                })
              ->whereDate('bookings.bookExpire', '>',$checkOutDate_1)
              ->get();

我的问题是如何按 bookingID 对房间进行分组并将该组集成到查询中以在入住和退房时获得可用房间。

日期 2018-02-08 - 2018-02-09 的

期望结果

standard room - 3 Rooms available
quad room - 5 rooms available
family room - 4 rooms available
king room - 3 rooms available

最佳答案

你只需要一个 simple 范围交集查询。应该这样做:

SELECT category, COUNT(*)
FROM room
WHERE NOT EXISTS (
    -- room is booked on the requested dates (...not)
    SELECT 1
    FROM reservedRoom
    JOIN bookings ON reservedRoom.bookingID = bookings.bookingID
    WHERE reservedRoom.roomID = room.roomID
    AND '2018-02-09' > checkIndate
    AND '2018-02-08' < checkOutDate
)
GROUP BY category

关于mysql - 当一次预订中有多个类别时,获取两个日期之间的可用房间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54586901/

相关文章:

mysql从另一列中选择列值

mysql - 安装 MySQL 驱动程序后,Perl SQLite 驱动程序停止工作

mysql - 为什么根据查询的日期范围不使用created_at索引?

mysql - 如何处理多个连接

mysql - 如何在另一个表中使用数据类型字符串作为外键创建无主键

python - Flask foreign_keys 仍然显示 AmbiguousForeignKeysError

sql - 如何在 Microsoft Access 中注释掉 SQL 代码?

MySql 删除大表中的少量记录很慢

php - laravel 5.4 中的三个加入和分组

php - 如何加入 Laravel 获取数据?