基本上我从房间表中检索所有可用房间的到达和离开日期。
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'
显示两个结果
感谢。
答案 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
);
您还可以通过以下方式重新表述:
在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'