Hi,
I am working hotel reservation project, I have a problem in searching room in the following table.
Table customer:
Client_id(pk) firstname lastname
praveenfds Praveen kumar
Table Reservationdetials:
member_id (pk) client_id(fk) start_date end_date
1001 praveenfds 2011-10-17 00:00:00 2011-10-20 00:00:00
Table Reservation:
reserve_id (pk) member_id(fk) room_id(fk)
RCV1 1001 ROOM101
Table room:
room_id(pk) room_number room_categId(fk)
ROOM101 101 RC1
ROOM102 102 RC1
ROOM103 103 RC1
ROOM104 104 RC1
ROOM105 105 RC1
ROOM201 201 RC2
ROOM202 202 RC2
ROOM301 301 RC3
ROOM302 302 RC3
Table roomcategory:
room_categId(pk) room_category room_rate persons_allowed
RC1 Standard 2500.00 3
RC2 Deluxe 3500.00 4
RC3 Suites 4500.00 5
My query is
room ROOM101 is booked on 17-10-2011 to 20-10-2011 by praveenfds(client_id) in the reservationdetails and reservation table mentioned above.
SELECT rooms.room_id,cat.room_category,cat.room_rate,cat.persons_allowed
FROM room rooms
INNER JOIN roomcategory cat ON rooms.room_categId = cat.room_categId
WHERE cat.room_category = 'Standard'
AND rooms.room_id not IN (SELECT t1.room_id
FROM room t1
INNER JOIN reservation t2 ON t1.room_id = t2.room_id
INNER JOIN reservationdetails t3 ON t2.member_id = t3.member_id
WHERE not(('2011-10-16' between t3.start_date and t3.end_date) and
('2011-10-21' between t3.start_date and t3.end_date)))
Result for the above query:works
ROOM102 Standard 2500.00 3
ROOM103 Standard 2500.00 3
ROOM104 Standard 2500.00 3
ROOM105 Standard 2500.00 3
but if i give
(SELECT t1.room_id
FROM room t1
INNER JOIN reservation t2 ON t1.room_id = t2.room_id
INNER JOIN reservationdetails t3 ON t2.member_id = t3.member_id
WHERE not(('2011-10-17' between t3.start_date and t3.end_date) and
('2011-10-18' between t3.start_date and t3.end_date)))
it displays all the rooms since given date is booked.
I tried > and < also but not getting the solution.
Please solve my query, if this logic is incorrect please provide any suggestions.
Thanks
Praveen
Loading
Javeed M ShaikhPosted Oct 11, 2011, 11:21 PM
I think I finally got this, please try the following:
SELECT rooms.room_id,cat.room_category,cat.room_rate,cat.persons_allowed
FROM room rooms
INNER JOIN roomcategory cat ON rooms.room_categId = cat.room_categId
WHERE cat.room_category = 'Standard'
AND rooms.room_id
NOT IN (SELECT t1.room_id
FROM room t1
INNER JOIN reservation t2 ON t1.room_id = t2.room_id
INNER JOIN reservationdetails t3 ON t2.member_id = t3.member_id
where start_date <='2011-10-24' and end_date >='2011-10-21')
Remember that the date compared with start_date is the enddate variable and with the end_date is the startdate variable.
Please do not forget to mark "Accepted Answer".
praveen bahubaliPosted Oct 12, 2011, 2:40 AM
This works....
praveen bahubaliPosted Oct 11, 2011, 5:13 PM
This will work only the dates from 17-10-2011 to 20-10-2011.
But when we think logic wise if the hotel is booked on 17 to 20, and some one try to book hotel on 15 to 18 or 19 to 21. it should not show right!!!
Javeed M ShaikhPosted Oct 11, 2011, 5:01 PM
Please try this and let us know:
Please do not forget to mark "Accepted Answer".
Javeed M ShaikhPosted Oct 11, 2011, 4:36 PM
The script with data is attached by Praveen, just scroll up a few post.
Suthish NairPosted Oct 11, 2011, 4:12 PM
praveen bahubaliPosted Oct 11, 2011, 3:04 PM
But i want both conditions to be satisfied in this query.
if the inputs not in booked dates, it should show all the rooms.
ex:
2011-10-13 and 2011-10-16
2011-10-21 and 2011-10-24
Result:
ROOM101 Standard 2500.00 3
ROOM102 Standard 2500.00 3
ROOM103 Standard 2500.00 3
ROOM104 Standard 2500.00 3
ROOM105 Standard 2500.00 3
praveen bahubaliPosted Oct 11, 2011, 2:12 PM
Query1 is perfect for this input
(('2011-10-16' between t3.start_date and t3.end_date) and ('2011-10-21' between t3.start_date and t3.end_date))
since ROOM101 is booked on 2011-10-17 to 2011-10-20
Result:(Query1)
ROOM102Standard2500.003
ROOM103Standard2500.003
ROOM104Standard2500.003
ROOM105Standard2500.003
i want the same result(Query1) for the following input 2011-10-17 and 2011-10-19
And also if the inputs not in booked dates, it should show all the rooms.
ex:
2011-10-13 and 2011-10-16
2011-10-21 and 2011-10-24
Result:
ROOM101 Standard 2500.00 3
ROOM102 Standard 2500.00 3
ROOM103 Standard 2500.00 3
ROOM104 Standard 2500.00 3
ROOM105 Standard 2500.00 3
But i am not getting this result.
Javeed M ShaikhPosted Oct 11, 2011, 1:00 PM
Following is the output
With the following Query2:
Following is the output
No Records Found.
But with the following query3:
Following is the output:
ROOM101
Please elaborate on what output is expected in Query 2
Please do not forget to mark "Accepted Answer".
praveen bahubaliPosted Oct 11, 2011, 12:03 PM
Thanks in advance, i have attached the file here.
Thanks
Praveen
Javeed M ShaikhPosted Oct 11, 2011, 11:43 AM
Please do not forget to mark "Accepted Answer".
praveen bahubaliPosted Oct 11, 2011, 4:39 AM
Javeed M ShaikhPosted Oct 11, 2011, 4:25 AM
Please try using conversion of date in your where clause by using CAST and try again.
Please do not forget to mark "Accepted Answer".