我上面有可用日期和价格的mysql表。第二张桌子包括房间细节。如何连接两个表格以获取两个日期之间的可用房间,而不会获得重复的内容。
答案 0 :(得分:2)
SELECT available.* , rooms.* FROM available, rooms
WHERE available.room_id = rooms.room_id AND
available.dt BETWEEN '2005-01-01' AND '2005-12-31'
答案 1 :(得分:2)
这里很难为您提供完整的答案,因为您只向我们展示了包含预订的表格 - 我们无法知道可用的房间范围。
返回至少部分所选时间段内预订房间的room_id的SQL可能是:
SELECT `room_id` , COUNT(*)
FROM `bookings`
WHERE `dt` BETWEEN "[start date]" AND "[end date]"
GROUP BY `room_id`;
如果您有一个房间表(而不是预订),您可以返回在此期间未预订的任何房间的列表:
SELECT `id`
FROM `rooms`
WHERE `id` NOT IN (
SELECT DISTINCT( `room_id` )
FROM `bookings`
WHERE `dt` BETWEEN "[start date]" AND "[end date]"
);
根据OP的反馈,现在的假设是:
dt
栏开始到第二天结束(即酒店房间)的一段时间内可用的房间详情因此,修改后的代码是:
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]"));
答案 2 :(得分:1)
假设您向我们展示的表名为rooms_dates
,其他表格为rooms
和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'
);