I have these three tables:
| hotel |
|---|
| hotel_id |
| hotel_name |
| room |
|---|
| room_id |
| hotel_id |
| room_price |
|---|
| room_id |
| date |
| price |
I know how to query the database to get the price for a given room_id and given date for the hotel stay. So I can do it, however, when it comes to doing this for a long date range, this becomes impractical.
You could either create your date series dynamically, as shown in the answer suggested by Bagus Tesa, or maintain a calendar table. Given that dates play a big part in booking/reservations apps, I would suggest creating a simple calendar table.
For this example I am using a calendar table with just a single dt (
DATE) column, which you can easily populate with a stored proc (or any other method you choose):Then it is a simple case of a
CROSS JOINbetween the dates for the booking and rooms, and a correlated subquery to retrieve the price per room per day:Note: although the keyword
CROSShas no significance in MySQL (it's just an INNER join with no ON clause), I think it makes the intent clear.Here's a db<>fiddle.