views:

102

answers:

3

alt text

I have above mysql table with available dates and prices. Second table includes room details. How can I join two tables to get available rooms between two dates and not get duplicate content.

+1  A: 

Assuming the table you showed us is called rooms_dates with two other tables rooms and room_details:

select room.id, room_details.xxxxxxx from rooms
inner join room_details on rooms.id = room_details.room_id
where rooms.id in
  (
    select distinct room_id from rooms_dates
    where dt >= 'xxxx-xx-xx' and dt <= 'yyyy-yy-yy'
  );
Matt Huggins
+2  A: 
SELECT available.* , rooms.* FROM available, rooms 
WHERE available.room_id = rooms.room_id AND 
available.dt BETWEEN '2005-01-01' AND '2005-12-31'
NAVEED
+2  A: 

This is hard to come up with a complete answer for you here, as you are only showing us the table which contains the bookings - we cannot know what range of rooms are available.

SQL which returns the room_id's for rooms which are booked for at least part of the selected period could be:

SELECT `room_id` , COUNT(*)
FROM `bookings`
WHERE `dt` BETWEEN "[start date]" AND "[end date]"
GROUP BY `room_id`;

If you had a table of rooms (rather than bookings), it would be possible for you to return a list of any rooms not booked during that period with:

SELECT `id`
FROM `rooms`
WHERE `id` NOT IN (
  SELECT DISTINCT( `room_id` )
  FROM `bookings`
  WHERE `dt` BETWEEN "[start date]" AND "[end date]"
);

AMENDMENT

Based on the feedback by OP, the assumptions are now:

  • The table contains details of rooms which are available for a period starting on the date in column dt and ending the following day (ie hotel rooms)
  • The query should return any rooms which are available for the entirity of the period entered (so only rooms which are available from DAY A to DAY B will be returned.

As such, the amended code is:

SELECT room_id
FROM available_rooms
WHERE dt BETWEEN "[start date]" AND DATE_SUB("[end date]",INTERVAL 1 DAY)
GROUP BY room_id
HAVING COUNT(*)=ABS(DATEDIFF("[start date]","[end date]"));
Lucanos
Sorry, above table is available dates only and the second table contain room details.
Stipe
Thanks Lucanos. It works more than excellent. I modified to join room table with details and now it looks like this: SELECT *,room_id FROM availability LEFT JOIN rooms ON availability.room_id=rooms.id WHERE active='1' AND city='".$_REQUEST['city']."' AND dt BETWEEN '".$_REQUEST['from']."' AND DATE_SUB('".$_REQUEST['to']."',INTERVAL 1 DAY) GROUP BY room_id HAVING COUNT(*)=ABS(DATEDIFF('".$_REQUEST['from']."','".$_REQUEST['to']."')) - I hope it's ok.
Stipe
Happy to help. But I will repeat a warning you will hear time and time again here on SO - Make sure you sanitise any parameters which you use in a SQL Statement to protect from SQL Injection Attacks, etc. Do a quick Google search for "SQL Injection MySQL PHP" and read a few of the tutorials - it will save you alot of grief in the future.
Lucanos
Thanks. I'm just starting with OOP and for now I'm using http://www.ricocheting.com/code/php/mysql-database-class-wrapper-v3 and I must say is really good. Once again for your help, don't know what to said. If I can help somehow just tell me!!!
Stipe