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

时间:2019-02-08 06:12:42

标签: mysql sql laravel eloquent

我的问题是,当预订在一个预订中可以有多种房型时,我迷失了在两个日期之间获得可用房间时该怎么办。

我有3张桌子:

保留房间

    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对房间进行分组并将该组集成到查询中,以在checkIn和checkOut获得可用的房间。

所需结果的日期为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

3 个答案:

答案 0 :(得分:1)

这里是如何进行查询的示例

第一步,找出当前房间数,按预订类别分组

此后,从房间表中减去按类别值和按类别分组的总房间数

    var data = {
    "strSelectedIds": selectedArray.join(',')
    };
    var countofProcessedRecords = selectedArray.length;
    $("#divLoader").removeClass("display-none");
    $.ajax({
    url: UrlSettings.ProcessSubscriptioData,
    type: 'POST',
    contentType: "application/json; charset=utf-8",
    dataType: "html",
    data: JSON.stringify(data),
    success: function (response) {
    sessionStorage.successMessage= true;
        location.reload();

        setTimeout(function () {
            $("#divLoader").addClass("display-none");
        }, 500);

    },
    error: function (e) {
        $("#divLoader").addClass("display-none");
    }
});

$( function () {
        if ( sessionStorage.successMessage) {
             $("#msg").append(
            '<div class="alert alert-success hideit alertSuc">' + countofProcessedRecords + 'subscriptions uploaded successfully.</div >');
            sessionStorage.successMessage = false;
        }
    } 
);

这是dbfiddle链接

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=8a91f2486bac25bda3f46b6ae3cddf8c

答案 1 :(得分:1)

您只需要一个 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

答案 2 :(得分:1)

您的sql请求将会

select r.category, count(*) as [count]
from room as r
    left join reservedRoom as rr
        on r.roomID = rr.roomID
    left join bookings as b
        on rr.bookingID = b.bookingID
            and ((b.checkIndate >= @DateFrom and b.checkIndate < @DateTo)
                or (b.checkOutDate > @DateFrom and b.checkOutDate <= @DateTo)
            )
where b.bookingID is null
group by r.category