酒店预订在抵达日期和出发日期之间检查空房间?

时间:2016-08-25 13:03:02

标签: mysql

基本上我从房间表中检索所有可用房间的到达和离开日期。

SQL查询

SELECT * FROM `roomcalendar` where day BETWEEN '2016-08-25' and '2016-08-31'

结果是

id day        roomname avaroom
 1 2016-08-25 RoomA          0
 2 2016-08-26 RoomA          2
 3 2016-08-27 RoomA          0
 4 2016-08-28 RoomA          1
 5 2016-08-29 RoomA          1
 6 2016-08-30 RoomA          0
 7 2016-08-31 RoomA          1

如果avaroom不等于0到达日期和出发日期,我只想要可用空间

我想要结果

SQL查询

SELECT * FROM `roomcalendar` where day BETWEEN '2016-08-25' and '2016-08-31'

必须为空结果

SQL查询

 SELECT * FROM `roomcalendar` where day BETWEEN '2016-08-25' and '2016-08-26'

必须为空结果

SQL查询

 SELECT * FROM `roomcalendar` where day BETWEEN '2016-08-26' and '2016-08-27'

它显示了一个结果

SQL查询

SELECT * FROM `roomcalendar` where day BETWEEN '2016-08-26' and '2016-08-28'

必须为空结果

SQL查询

SELECT * FROM `roomcalendar` where day BETWEEN '2016-08-28' and '2016-08-30'

显示两个结果

感谢。

2 个答案:

答案 0 :(得分:1)

  

如果avaroom不等于0到达日期和出发日期,我只想要可用空间

所以,如果你重新制定这个:

  • 如果两个日期之间没有avaroom = 0的行,则需要房间。

在SQL中翻译:

select * from roomcalendar
where day between '2016-08-25' and '2016-08-31'
and not exists (
  select * from roomcalendar
  where day between '2016-08-25' and '2016-08-31'
  and avaroom = 0
);

您还可以通过以下方式重新表述:

  • 如果avaroom不是0的行数是预期的总行数(在这种情况下,这相当于天数),你想要房间。

在SQL中翻译:

select * from roomcalendar
where day between '2016-08-25' and '2016-08-31'
and avaroom != 0
having count(*) = datediff('2016-08-31','2016-08-25') + 1

您可能需要稍微调整一下查询,但这应该指向正确的方向。

答案 1 :(得分:0)

像这样修改查询。

SELECT * FROM `bookings` WHERE `room_id` = '1' 
AND booking_end >= '2020-12-01 13:20:00' AND booking_start <= '2020-12-10 13:20:00'