简体   繁体   English

mysql检查房间可用性查询中的下一个和上一个可用时间

[英]mysql check next and previous available period in room availability query

for a project of mine I would like to add the feature to show the previous and the next available period in a booking reservation system. 对于我的一个项目,我想添加该功能以显示预订预订系统中的上一个和下一个可用时间段。

I have this query that checks if the house is available: 我有此查询,检查房子是否可用:

SELECT id
  FROM tbl_reservations
  WHERE (
    `status` LIKE 'confirmed' OR `status` LIKE 'total-payed'
   ) AND (
     '2015-08-12' BETWEEN checkin AND checkout 
     OR checkout BETWEEN '2015-08-12' AND '2015-08-13'
     OR '2015-08-12' BETWEEN checkin AND checkout
     OR checkin BETWEEN '2015-08-12' AND '2015-08-13'
   );

If it finds a reservation in that period, the PHP script return false, so it shows a message like 如果在该期间找到保留,PHP脚本将返回false,因此显示如下消息

"Sorry, no availability in that selected period" “对不起,在所选期间内没有空房”

I would like to show also something like: 我还想显示类似的东西:

"The next available period for a stay of XX nights is: " “下一个可供XX晚住宿的时间是:”

and then show the checkin and the checkout date. 然后显示签到和签出日期。

Same thing for the previous period . 上期相同。

I am not mastering sql 100%, so I am here asking for suggestions of any kind to point me in the right direction. 我不是精通sql的100%,因此我在这里寻求任何建议以指出正确的方向。

Here are the two solutions. 这是两个解决方案。

Get next available reservation for the expected stay: 获取预期住宿的下一个可用预订:

SELECT id
FROM tbl_reservations
WHERE (
  `status` != 'confirmed' and `status` != 'total-payed'
 )
and checkin = (select min(checkin) from tbl_reservations
               where checkin > '2015-08-12'
                 and checkout - checkin >= '2015-08-12' - '2015-08-13');          

Get previous reservation for the expected stay. 提前获得预期的住宿预订。

SELECT id
  FROM tbl_reservations
  WHERE (
    `status` != 'confirmed' and `status` != 'total-payed'
   )
  and checkin = (select max(checkin) from tbl_reservations
                 where checkin < '2015-08-12'
                 and checkout - checkin >= '2015-08-12' - '2015-08-13');

These can easily be combined into one query, but from the question it looked like you wanted two separate queries. 这些可以很容易地组合成一个查询,但是从这个问题看来,您想要两个单独的查询。 These are tested and ready to go.. 这些已经过测试,随时可以使用。

I have solved this way: 我已经解决了这种方式:

As a visitor is interested mainly in the period he/she has selected, I run the same query for a maximun of 30days before and 30days after the original checkin date. 由于访客主要对他/她选择的时期感兴趣,因此我对原始签入日期之前和之后的30天(最长)运行相同的查询。

I break the loop as soon as I get a "false", that means NO RESERVATIONS in theat period. 我一收到“假”就中断循环,这意味着在剧院期间没有预订。

Maybe it is not the most elegant way or the most performant, but it's working and I am happy :) 也许这不是最优雅的方式,也不是最高效的方式,但是它正在起作用,我很高兴:)

Any further suggestion is still accepted. 任何进一步的建议仍被接受。 Bettering, always! 永远更好!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM