
Hello to all,
I need to create a search query for a house that matches the data entered in the database: the date they want to move and leave, the number of people they have in the group, and the price per night.
Let's say the user was looking for a house:
dates: from 2011-01-15 to 2011-03-01 (see image period A1C1), for 3 people, and he is willing to spend from 90 to 125 US dollars per night.
These are my manual calculations for this search:
- dates available in the database
- Total dates the user wants to stay: 44 days
- The price for the first period 2011-01-15 to 2011-01-25 is 10 days * $ 100 = $ 1000
- The price for the second period 2011-01-25 to 2011-02-14 is 20 days * $ 120 = $ 2400
- The price for the third period 2011-02-14 to 2011-03-01 is 14 days * $ 140 = $ 1960
- total average price per night = 1000 + 2400 + 1960/44 = $ 121.8
- the price and number of people matches user input, so we show this house
If you combine the dates and calculate the average price per night for a given period, the search script should match the data array above.
My question is: What should my query look like in order to quickly calculate if user data matches records in the database .
I thought about using the SQL DATEDIFF function and then multiplied for the price ... etc, but it looks pretty complicated.
I would be grateful for any advice.
thanks
UPDATE
Here is my database schema:
Apt_search_periods table that stores all the combined dates (continuous dates from the availability table)
+-----------+------------+------------+-----------+--------------+--------+ | period_id | start_date | end_date | rental_id | nb_of_people | merged | +-----------+------------+------------+-----------+--------------+--------+ | 21 | 2011-03-31 | 2012-03-31 | 548 | 4 | y | +-----------+------------+------------+-----------+--------------+--------+
Apt_search_periods_avail table associated with merged dates with availability table
+----+-----------+-----------------+ | id | period_id | availability_id | +----+-----------+-----------------+ | 21 | 21 | 20953 | | 22 | 21 | 20952 | | 23 | 21 | 4033 | +----+-----------+-----------------+
Availability table with advanced dates and prices
+-------+-----------+------------+------------+--------------+--------------+ | id | rental_id | start_date | end_date | nb_of_people | rent_per_day | +-------+-----------+------------+------------+--------------+--------------+ | 20952 | 548 | 2011-03-31 | 2011-07-01 | 4 | 575 | | 4033 | 548 | 2011-07-01 | 2011-09-01 | 4 | 680 | | 20953 | 548 | 2011-09-01 | 2012-03-31 | 4 | 575 | +-------+-----------+------------+------------+--------------+--------------+