barricades — 2013-07-31T06:21:20-04:00 — #1
I'm not so great with sql but I think what I want should be possible, I'm just not sure how to do it.
I have a site which allows users to list their youth hostel. Potential customers can then browse these hostels and place bookings.
Tables: (not including id's and other cols!)
When users search the site I allow them to select a checkin date, a checkout date, a place name, and the number of guests. Note that each hostel has a maximum number of guests which they can accommodate, for example 20.
So say previously a booking has been made at a particular hostel for 8 people during the dates selected for the search, then I need to know that the maximum number of guests that particular hostel can accommodate between those dates is now 12, and it would only appear in searches for <= 12 guests.
Likewise if that hostel had a couple more bookings and was at it's maximum capacity, then it shouldn't turn up in the search at all.
At the moment I have a query which gets all the hostels but if there's any bookings for a hostel between the selected dates, then it's excluded from the results. I'd like to be able to get all the bookings for each hostel during the selected dates, count the numbers of guests booked in, and if that number subtracted from the max_guests of the hostel gives a result which is lower than the number of guests specified in the search, then exclude it from the results.
at the moment my query looks like:
bookings ON (
User ON (
address_one LIKE '%london%') OR (
address_two LIKE '%london%') OR (
city LIKE '%london%'))
maxguests >= 4
AND NOT EXISTS (SELECT * FROM bookings WHERE
id AND NOT('2013-07-31' >
checkout OR '2013-08-02' <
Can anyone tell me how I could modify that query to make it do what I need?
barricades — 2013-08-01T11:54:20-04:00 — #2
In case it encourages anyone to help me out I've stuck together a sqlfiddle: http://sqlfiddle.com/#!2/300f3/1
and a better description of my problem might be:
two tables: Hostels and Bookings.
Hostels have a maximum number of guests which they can accommodate. Bookings are made between two dates and have a column 'guests' for the number of guests in the booking.
Users should be able to search for a hostel given an area, a checkin date, a checkout date and the number of guests in the party.
My query at the moment finds all the hostels which don't have a booking between the dates but what I actually want is:
The query to return all hostels where, if the hostel has bookings during the user specified period, then the sum of the guests involved in those bookings, subtracted from hostel.maxguests shows that there is enough space left in the hostel. IE the number needs to be more than the user specified guests number from the search.
ramasaig — 2013-08-16T19:47:24-04:00 — #3
I won't claim to have looked at this in detail, but could you have something in your WHERE clause along the lines of:
"WHERE ....max_beds - booked_beds >= number_in_party.."
Of course you need other stuff in the WHERE clause to limit the dates etc.
system — 2014-10-08T00:43:34-04:00 — #4
This topic is now closed. New replies are no longer allowed.