我是SQL和Access的新手,想知道你能不能帮忙吗?
我正在使用Access 2010创建酒店预订数据库,但我无法在我搜索空房间的地方工作。
我的数据库有5个表格如下(括号中的字段名称):
BOOKINGS (BookRef, CustAcctNo, BookDate, ArrivDate, DurStay, EmpNo, RoomNo)
CUSTOMERS (CustAcctNo, Title, Forename, Surname, Address1, Address2, Address3)
EMPLOYEES (EmpNo, Title, Forename, Surname)
ROOM TYPES (RoomType, Description, Rate/Price)
ROOMS (RoomNo, RoomType)
这些表格都有一对多的表格。关系,即一个客户可以有很多预订。
所以,我的想法是感兴趣的领域将是ArrivDate Field(到达日期)和DurStay(Stay of Stay)字段。在房间表中,房间号码是我呼叫的字段。
所以,我到目前为止最接近的是:
PARAMETERS [Start Date] DateTime, [End Date] DateTime;
SELECT R.*, [Start Date] AS Expr1, [End Date] AS Expr2, *
FROM ROOMS AS R
LEFT JOIN (SELECT B.RoomNo
FROM Bookings AS B
WHERE ([Start Date] between B.ArrivDate and (B.ArrivDate + [Please Enter]))
AND ([End Date] between B.ArrivDate and (B.ArrivDate + B.DURSTAY))) AS BKD
ON R.RoomNo = BKD.RoomNo
WHERE (((BKD.RoomNo) Is Null));
这根本不适合我。我已经尝试了很多次上面代码的不同版本,但似乎无处可去。我的想法是,我在一个搜索中,在日期和住宿期间之间的字段为空,但也许我会以错误的方式进行搜索,我不确定。
希望我在这里提供了足够的细节,但如果您需要了解更多,请告诉我。我真的很感谢你们至少看过这个。也许对它的新观点可能会发现我出错的地方。
非常感谢您提供任何帮助。
答案 0 :(得分:0)
尝试改变:
WHERE ([Start Date] between B.ArrivDate and ...
AND ([End Date] between B.ArrivDate and ...
到此:
WHERE ([Start Date] between B.ArrivDate and ...
OR ([End Date] between B.ArrivDate and ...
我认为这更符合逻辑,因为它接受部分匹配,即日期重叠。
答案 1 :(得分:0)
PARAMETERS [Start Date] DateTime, [End Date] DateTime;
SELECT R.*, [Start Date] AS Expr1, [End Date] AS Expr2, *
FROM ROOMS AS R
LEFT JOIN (SELECT B.RoomNo
FROM Bookings AS B
WHERE ([Start Date] between B.ArrivDate and (B.ArrivDate + **B.DURSTAY**))
**OR** ([End Date] between B.ArrivDate and (B.ArrivDate + B.DURSTAY))) AS BKD
ON R.RoomNo = BKD.RoomNo
WHERE (((BKD.RoomNo) Is Null));
或 一个更容易,更优雅的查询将是:
SELECT R.*, #06/05/2014# as [Start Date] , #06/10/2014# as [End Date]
FROM ROOMS AS R
WHERE R.RoomNo not in
(
SELECT DISTINCT RoomNo FROM Bookings B
WHERE (#06/05/2014# between B.Arrival and (B.Arrival + B.Duration))
OR (#06/10/2014# between B.Arrival and (B.Arrival + B.Duration))
);
日期参数必须以美国格式(#MM / DD / YYYY#)
传递