简体   繁体   中英

Mysql query to get hotel room availability

I'm implementing a booking platform I have 3 tables:

  • "hotel" - to hold the hotel information
  • "hotel_room" - to hold room info per hotel
  • "hotel_room_price" - have the availability by date, number of rooms available and price

I want to search by start date and end date, local and number of rooms (each room have the number of adults and number of child)

Here is some example of my tables:

CREATE TABLE `hotel` (
  `id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `rating` smallint(6) NOT NULL DEFAULT '3' COMMENT '0 - Not Rated | 1 - One Star | 2 - Two Stars | 3 - Three Stars | 4 - Four Stars | 5 - Five Stars',
  `local` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `hotel_room` (
  `id` int(11) NOT NULL,
  `hotel_id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `max_capacity_adult` smallint(6) NOT NULL,
  `max_capacity_child` smallint(6) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `hotel_room` ADD CONSTRAINT `fk_hotel_room_hotel_id` FOREIGN KEY (`hotel_id`) REFERENCES `hotel` (`id`) ON DELETE CASCADE;

CREATE TABLE `hotel_room_price` (
  `id` int(11) NOT NULL,
  `hotel_room_id` int(11) NOT NULL,
  `price_adult` decimal(20,2) DEFAULT NULL,
  `price_child` decimal(20,2) DEFAULT NULL,
  `quantity` int(11) NOT NULL DEFAULT '1' COMMENT 'available rooms, 0 if there is no more available',
  `date` date NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `hotel_room_price` ADD CONSTRAINT `fk_hotel_room_price_hotel_room_id` FOREIGN KEY (`hotel_room_id`) REFERENCES `hotel_room` (`id`) ON DELETE CASCADE;

What is the better approach get the available rooms when a user search, one note it is possible to search for multiple rooms for example:

start_date = 2019-06-25
end_date = 2019-06-29
local = "Tomar"
Room=[
  [
     nr_adults = 2,
     nr_children=1
  ],
  [
     nr_adults = 4,
     nr_children=0
  ]
]

I think first thing to do it check only hotels from the right local then check if the room can hold the number of adults and children if yes check for availability. I'm with lot of problems to create a query or multiple queries to handle this in the right way.

You can check and example of my database here http://sqlfiddle.com/#!9/458be2c

Here is query that selects all room ID's available in a given time frame. In this instance I picked June 26- June 28. This should be a good starting point for the rest of the query.

SELECT hotel_room_id
FROM hotel_room_price
WHERE date between '2019-06-26' AND '2019-06-28'
AND quantity > 0
GROUP BY hotel_room_id
HAVING COUNT(*) > DATEDIFF('2019-06-28', '2019-06-26')

Here is a somewhat hacky query to get some of the information about the rooms. Note there is not functionality for searching for multiple rooms in this sample:

SELECT h.name AS Name, h.rating AS Rating, sq.name AS Type
FROM hotel h
INNER JOIN
(SELECT * 
FROM hotel_room
WHERE hotel_room.id IN
(SELECT hotel_room_id
FROM hotel_room_price
WHERE date between '[START DATE]' AND '[END DATE]'
AND quantity > 0
GROUP BY hotel_room_id
HAVING COUNT(*) > DATEDIFF('[END DATE]', '[START DATE]'))
AND max_capacity_child >= [CHILD COUNT]
AND max_capacity_adult >= [ADULT COUNT]) sq
ON h.id = sq.hotel_id
WHERE h.local = "[LOCATION]"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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