0

您将如何编写查询以从预订数据库中选择 2012 年 5 月 1 日至 2012 年 11 月 1 日之间预订的房间?

4

3 回答 3

3
select r.* from rooms r 
left outer join bookings b on b.roomno = r.roomno
where b.dateto not between '2012-05-01' and '2012-11-01'
and b.datefrom not between '2012-05-01' and '2012-11-01'
or b.roomno is null

SQLFiddle 示例

于 2012-04-28T07:39:11.163 回答
2

这应该包括在两个给定日期之间未预订的所有房间:

SELECT * FROM Room r
JOIN Booking b ON b.hotelNo = r.hotelNo AND r.roomNo = b.roomNo
WHERE NOT ('2012-05-01' BETWEEN (b.dateFrom AND b.dateTo)
OR '2012-11-01' BETWEEN (b.dateFrom AND b.dateTo))
于 2012-04-28T07:48:39.477 回答
1

请尝试以下

SELECT * from room
WHERE (roomNo, hotelNo) not in
( select roomNo, hotelNo
  from booking
  where datefrom between '2012-05-01' and '2012-11-01'
  or dateto between '2012-05-01' and '2012-11-01'
)
于 2012-04-28T07:41:20.300 回答