-1

我有这张桌子Table1

Room - StartDate - EndDate
1    - 01/01/2013  05/01/2013
1    - 14/01/2013  15/01/2013
2    - 10/01/2013  13/01/2013

我想显示从 2013 年 2 月 1 日到 2013 年 3 月 1 日所有可用的“房间”

我用:

select * 
from Table1 
where ('02/01/2013' NOT BETWEEN table1.stardate AND table1.enddate) 
   OR ('03/01/2013' NOT BETWEEN table1.startdate AND table1.enddate)

但是查询结果显示2(这是正确的)但也显示1,因为最后一条记录

我该怎么做才能只显示“房间 2”?

谢谢

4

3 回答 3

2

我想你已经有另一张桌子了Rooms

所以,如果Table1是一个Reservations表:

SELECT  r.* 
FROM    Rooms AS r
WHERE   NOT EXISTS
        ( SELECT    *
          FROM      Table1 AS res
          WHERE     res.Room = rooms.RoomID
            AND     '20120102' < res.EndDate 
            AND     res.StartDate < '20120103' 
        ) ;
于 2013-05-25T08:33:28.933 回答
0

如果 enddate 始终 > startdate,则更OR改为AND

select * 
from   Table1
where  ('02/01/2013' NOT BETWEEN table1.startdate AND table1.enddate)
AND    ('03/01/2013' NOT BETWEEN table1.startdate AND table1.enddate)
于 2013-05-25T08:23:04.550 回答
0

尝试这个

Select * from Table1 

where(stardate >='02/01/2013' and enddate <= '03/01/2013')
于 2013-05-25T10:46:43.497 回答