简体   繁体   中英

mysql query display remaining room type in an online hotel reservation

I'm working on this online reservation project and I am stock in this part. Here's the scenario. database structure:

tbl_guestinfo
   guest_id
   name
   checkin
   checkout
   numAdults
   numChild

tbl_rooms
   room_id
   room_name
   guest_id
   type_id

tbl_roomtype
   type_id
   room_rate
   room_type
   noOf_Rooms

I can already get a good result from my query wherein I could scan all available rooms and display all my the information that I want.

SELECT *
FROM tbl_rooms, tbl_roomtype 
WHERE guest_id NOT IN
(
SELECT `guest_id` from tbl_guestinfo where
(`checkin` < ' $varCheckOut' and `checkout`>= '$varCheckOut' )
OR (`checkin`<= '$varCheckIn' and `checkout`> '$varCheckIn' )
OR (`checkin`>= '$varCheckIn' and `checkout`<='$varCheckOut')
)

What I need is to display all the remaining room_type that is available. So for example if there are 5 standard room and 5 deluxe room and 1 standard room is occupied by guest1 from 2012-10-01 to 2012-10-05 and 1 deluxe room occupied at the same date by guest2 then the site will be still displaying standard and deluxe room since there are 4 left for each of the room_type.

Note that I will be putting the room_type in a html table and noOf_Rooms left in a combo box.

Please help been researching and reading a lot but still can't figure it out. Thank you in advance for all your help.

Cheers

Extending your query:

 SELECT room_type, count(*)
 FROM tbl_rooms, tbl_roomtype 
 WHERE guest_id NOT IN
 (
   SELECT `guest_id` from tbl_guestinfo where
   (`checkin` < ' $varCheckOut' and `checkout`>= '$varCheckOut' )
   OR (`checkin`<= '$varCheckIn' and `checkout`> '$varCheckIn' )
   OR (`checkin`>= '$varCheckIn' and `checkout`<='$varCheckOut')
 )
 GROUP by room_type

I don't like that your room table contains the guest_id. Make something like a 'reservation' table that maps guests and rooms, and contains all the dates, etc.

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