简体   繁体   English

mysql 查询特定日期和时间段内酒店的客房供应情况

[英]mysql to check room availability in a hotel for a particular date and time duration

I need help to find out a query to check the availability of rooms in a hotel for a particular date and time duration.我需要帮助来找出一个查询,以检查特定日期和时间段内酒店房间的可用性。 I am explaining it below.我在下面解释。

Table "hotel_room_book" has the folloing columns.表“hotel_room_book”有以下几列。

CREATE TABLE IF NOT EXISTS `hotel_room_book` (
  `hotel_room_book_id` int(10) NOT NULL AUTO_INCREMENT,
  `hotel_room_id` int(10) NOT NULL,
  `hotel_id` int(10) NOT NULL,
  `who_has_booked` int(10) NOT NULL,
  `booked_for_whom` int(10) NOT NULL,
  `room_price` float(10,2) NOT NULL,
  `book_status` enum('0','1') NOT NULL DEFAULT '0',
  `booking_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `checkin_date` date NOT NULL,
  `checkin_time` int(4) NOT NULL,
  `checkout_date` date NOT NULL,
  `checkout_time` int(4) NOT NULL,
  PRIMARY KEY (`hotel_room_book_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=20 ;

INSERT INTO hotel_room_book ( hotel_room_book_id , hotel_room_id , hotel_id , who_has_booked , booked_for_whom , room_price , book_status , booking_date , checkin_date , checkin_time , checkout_date , checkout_time ) VALUES (1, 13, 1, 1, 1, 564564.00, '0', '2011-06-15 00:00:00', '2011-06-15', 3, '2011-06-17', 12), (2, 13, 1, 1, 1, 564564.00, '0', '2011-06-15 00:00:00', '2011-06-17', 16, '2011-06-18', 3), (3, 13, 1, 1, 1, 23.00, '0', '2011-06-01 00:00:00', '2011-06-19', 5, '2011-06-20', 18); INSERT INTO hotel_room_book ( hotel_room_book_id , hotel_room_id , hotel_id , who_has_booked , booked_for_whom , room_price , book_status , booking_date , checkin_date , checkin_time , checkout_date , checkout_time ) 值 (1, 13, 1, 1, 1, 564564.00, '11-', '02 -15 00:00:00', '2011-06-15', 3, '2011-06-17', 12), (2, 13, 1, 1, 1, 564564.00, '0', '2011- 06-15 00:00:00', '2011-06-17', 16, '2011-06-18', 3), (3, 13, 1, 1, 1, 23.00, '0', '2011 -06-01 00:00:00', '2011-06-19', 5, '2011-06-20', 18);

Means room_id 13 is booked for表示 room_id 13 已被预订

slno     checkin_date     checkin_time     checkout_date   checkout_time

 1        15-06-2011      3                 17-06-2011      12

 2        17-06-2011      16                18-06-2011      3

 3        19-06-2011      5                 20-06-2011      18

I am searching for我正在寻找

slno     checkin_date     checkin_time     checkout_date     checkout_time

1         17-06-2011       13                    17-06-2011       15

2        18-06-2011        4                     19-06-2011       4

3        14-06-2011        2                     15-06-2011       1

4        20-06-2011        19                    21-06-2011       2

I used the following logic for "room available" cases:对于“可用房间”案例,我使用了以下逻辑:

case1:(1,2,3 in above) Room is available case1:(上面的1,2,3)房间有空

if required checkin_date and checkin_time >= booked checkout_date and checkout_time如果需要 checkin_date 和 checkin_time >= booked checkout_date 和 checkout_time

and required checkout_date and checkout_time <= booked checkin_date and checkint_time并且要求 checkout_date 和 checkout_time <= 预订的 checkin_date 和 checkint_time

case2:(4 in above required date matches for availability) Room is available案例 2:(4 个以上要求的日期与可用性相匹配)房间可用

if required checkin_date and checkin_time <= booked checkin_date and checkin_time如果需要 checkin_date 和 checkin_time <= 预订的 checkin_date 和 checkin_time

and required checkout_date and checkout_time <= booked checkin_date and checkint_time并且要求 checkout_date 和 checkout_time <= 预订的 checkin_date 和 checkint_time

My query is correct if I check manually,but I get zero record which is obvious.如果我手动检查,我的查询是正确的,但我得到的是零记录,这很明显。 Hence can you please think on it and help me to find out the query which gives binary result yes/no or 1/0 for available/unavaible of that particular room during that particular period?因此,您能否考虑一下并帮助我找出在特定时期内该特定房间的可用/不可用的二进制结果为是/否或 1/0 的查询?

In Summary: Lets say room no 13 is booked from Dt-15-06-2011 at 3 to Dt-17-06-2011 at 12.总结:假设 13 号房间从 Dt-15-06-2011 3 点到 Dt-17-06-2011 12 点被预订。

Again it is booked from Dt-17-06-2011 at 16 to Dt-18-06-2011 at 3. That means room number 13 is available for the duration Dt-17-06-2011 at 13 to Dt-17-06-2011 at 15.Now my question is avalability of room 13 is very clear from manual check.But what it is the mysql for it to check programaticaly.再次从 Dt-17-06-2011 16 点到 Dt-18-06-2011 3 点预订。这意味着 13 号房间在 Dt-17-06-2011 13 点到 Dt-17-06 期间可用-2011 年 15 点。现在我的问题是 13 号房间的可用性从手动检查中非常清楚。但是它要以编程方式检查 mysql 是什么。

SELECT * FROM hotel_room_book a , hotel_room_book b WHERE a.checkout_date<= 
 $new_checkin_date  AND b.checkin_date >= $new_checkout_date AND a.hotel_id = 
 b.hotel_id AND a.hotel_room_id = b.hotel_room_id ;

And this will work only if you have checkin_date and checkout_date as TIMESTAMP instead of date, as checking date and time separately will involve complex computation.这只有在您将 checkin_date 和 checkout_date 作为 TIMESTAMP 而不是日期时才有效,因为分别检查日期和时间将涉及复杂的计算。 (Hope you understand the problem in storing date and time in separate columns) (希望您了解在单独的列中存储日期和时间的问题)

This query is Exactly Correct.这个查询是完全正确的。 ( Booking Date Column range) Just it running inverse. (预订日期列范围)正好相反。

 SELECT * FROM `bookings` WHERE `room_id` = 1  
 AND booking_end >= $from AND booking_start <= $to

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

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