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
hotel_room_occupied_bythat would store data like who's occupying the room, the date they started occupying the room, the date the room is expected to be vacant, that way i can check for room availability and length of stay for a guest. I'm just throwing around ideas - Kebab Programmer