Mysql 当一次预订中有多个类别时,在两个日期之间获得可用房间

Mysql 当一次预订中有多个类别时,在两个日期之间获得可用房间,mysql,sql,laravel,eloquent,Mysql,Sql,Laravel,Eloquent,我的问题是,当预订可以在一次预订中有多个房间类型时,在两个日期之间获得可用房间时,我不知道该怎么办 我有三张桌子: 储备量 房间 预订 我试过这个代码,但我不知道该怎么做 $availableRooms = booking::where(function($query) use ($checkInDate_1, $checkOutDate_1) { $query->where(function($query) use (

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

我有三张桌子:

储备量

房间

预订

我试过这个代码,但我不知道该怎么做

 $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();

下面是一个如何进行查询的示例

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

之后,用房间表中按类别分组的房间总数减去“按类别”值

   select x.category
          ,count(x.roomId) - case when isnull(max(booked_rooms.cnt))=1 then
                                       0
                                  else max(booked_rooms.cnt)
                                  end as available_rooms
     from room x
left join ( select c.category /* Find out the rooms bny category which are booked within the dates*/
                  ,count(c.roomID) as cnt
              from bookings a
              join reservedRoom b
                on a.bookingID=b.bookingID
              join room c
                on b.roomID=c.roomID
            where a.checkIndate between '2018-02-08' and '2018-02-09' 
            group by c.category
           )booked_rooms
        on x.category=booked_rooms.category
 group by x.category  
这是一个dbfiddle链接


下面是一个如何进行查询的示例

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

之后,用房间表中按类别分组的房间总数减去“按类别”值

   select x.category
          ,count(x.roomId) - case when isnull(max(booked_rooms.cnt))=1 then
                                       0
                                  else max(booked_rooms.cnt)
                                  end as available_rooms
     from room x
left join ( select c.category /* Find out the rooms bny category which are booked within the dates*/
                  ,count(c.roomID) as cnt
              from bookings a
              join reservedRoom b
                on a.bookingID=b.bookingID
              join room c
                on b.roomID=c.roomID
            where a.checkIndate between '2018-02-08' and '2018-02-09' 
            group by c.category
           )booked_rooms
        on x.category=booked_rooms.category
 group by x.category  
这是一个dbfiddle链接

您只需要一个范围交点查询。这应该做到:

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
您只需要一个范围交点查询。这应该做到:

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

您的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

您的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

你能用问题语句而不是hibernate编写sql查询吗?你能用问题语句而不是hibernate编写sql查询吗?重叠规则非常简单;如果事件A在事件B结束之前开始,而在事件B开始之后结束,则事件A与事件B重叠。重叠的规则很简单;如果事件A在事件B结束之前开始,而在事件B开始之后结束,则事件A与事件B重叠。这件事没有“或”。
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