简体   繁体   中英

room availability for multiple hotel in mysql

I am trying to create an online room reservation system.

One of the tables of the database is supposed to hold the bookings. It has an autonumber field, customer data fields, two date fields for arrival and departure, and a number field for the number of rooms booked.

A search page submits the arrival and departure dates to a result page which is then supposed to tell the customer how many rooms are available within the period if any. This is where it all goes wrong. I just can't get an accurate count of the number of rooms already booked within the period requested.

guest  | arrive     | depart      |booked |room_id
Smith  | 2017-06-20 | 2017-06-22  | 3     |1
Jones  | 2017-06-20 | 2017-06-21  | 2     |1
Brown  | 2017-06-22 | 2017-06-23  | 3     |2
White  | 2017-06-15 | 2017-06-20  | 4     |1

Room id comes from

id     |  type       | count   |hotelid
1      | Single room |  5      | 1
2      | Deluxe room |  8      | 1

If the roomid 1 has 5 rooms and 2 have 8, I want the result like this.

date        available     roomid
2017-06-20  |   0      |  1
2017-06-20  |   8      |  2
2017-06-21  |   2      |  1
2017-06-21  |   8      |  2
2017-06-22  |   5      |  1
2017-06-22  |   5      |  2

I am also using calender table. Please help me to find a solution

What about the following query? It returns the room type, date, available rooms (as in the total numer of rooms of this type, NOT the rooms left) and booked rooms, for each room type and date.

select b.roomid, c.dt, sum(r.count) as total_rooms, 
  coalesce(sum(b.booking_cnt), 0) as booked_rooms
from calendar_table as c
left join bookinglist b on c.dt between b.date_from and b.date_to
left join rooms as r on b.roomid = r.id
where c.dt between DATE_START and DATE_END
group by b.roomid, c.dt
order by b.roomid, c.dt

Then in the frontend, the columns total_rooms and booked_rooms can be subtracted to get the available rooms number.

Bear in mind that I have no schema nor data to test this query, but it should give you an idea about where to go next. Next time you can create an SQL Fiddle to make it easier to create an answer.

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