【问题标题】:Query to return available hotel rooms 3 days earlier or later than a specified range查询返回比指定范围早或晚 3 天的可用酒店房间
【发布时间】:2020-09-08 08:51:38
【问题描述】:

我想返回酒店预订系统中的可用房间。查询应返回比指定日期范围早或晚 3 天的结果。

数据库结构:

房间表(RoomID、RoomType、BedsNo、ViewAvailable...) 预订表(ReservationID、RoomID、CheckinDate、CheckoutDate...)

我正在使用以下查询返回指定日期范围内的可用房间:DateFrom 和 DateTo。但是,如何返回比日期范围 DateFrom 和 DateTo 早或晚 3 天的结果。

select * from room 
where room.roomId not in
(select reservation.roomId from reservation where 
  (reservation.check_in_date <= `FromDate` AND reservation.check_out_date >= `ToDate`) 
           OR (reservation.check_in_date < `FromDate` AND reservation.check_out_date >= `ToDate`) 
           OR (`FromDate` <= reservation.check_in_date AND `ToDate` >= reservation.check_out_date))

【问题讨论】:

  • 您是否尝试将日期减去/添加 3 天?

标签: mysql


【解决方案1】:

你不能通过类似的方式为FromDateToDate 添加 3 天

[...]
where reservation.check_in_data <= date_sub(FromDate, interval 3 day) 
and reservation.check_out_date >= date_add(ToDate, interval 3 day)
[...]

【讨论】:

    【解决方案2】:

    使用间隔

    select * from room 
    where room.roomId not in
    (select reservation.roomId from reservation where 
      (reservation.check_in_date <= `FromDate` - INTERVAL 3 DAY AND reservation.check_out_date >= `ToDate` + INTERVAL 3 DAY) 
               OR (reservation.check_in_date < `FromDate` - INTERVAL 3 DAY AND reservation.check_out_date >= `ToDate` + INTERVAL 3 DAY) 
               OR (`FromDate` - INTERVAL 3 DAY <= reservation.check_in_date AND `ToDate` + INTERVAL 3 DAY >= reservation.check_out_date))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-18
      • 2019-04-27
      • 2017-08-27
      • 2014-05-17
      • 1970-01-01
      • 1970-01-01
      • 2019-03-15
      • 1970-01-01
      相关资源
      最近更新 更多