Best unofficial Apache Server developers community
Username
Forgot password?
Sign in with Twitter account
Sign in with Facebook account

Mysql to check room availability in a hotel for a particular date and time duration

0

36 views

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.

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);

Means room_id 13 is booked for

slno     checkin_date     check_time     checkin_date     check_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     check_time     checkin_date     check_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

Hence my requirements must result "Available".

asked June 16, 2011 4:09 am CDT
posted via StackOverflow

1 Answer

0
 

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

answered June 16, 2011 4:23 am CDT

Your answer

Join with account you already have


Sign in with Twitter account
Sign in with Facebook account
Sign in with Google Friend Connect

Preview
Similar questions