0

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

  • You can use BETWEEN, probably do you better. Possible dupe, http://stackoverflow.com/questions/1080207/mysql-select-all-data-between-two-dates – wesside Sep 11 '12 at 17:52

1 Answers1

0

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.

Alain Collins
  • 16,268
  • 2
  • 32
  • 55