我希望在星期二中午12点找到100个或更多容量且免费(即未预订)的讲座的房间代码。
数据存储在roomBooking
表中,该表具有:
bookingID
roomCode
moduleCode
dayReq
timeReq
学期
classSize
到目前为止我的查询:
SELECT rCode, capacity
FROM room
INNER JOIN roomBooking
ON room.rCode = roomBooking.roomCode
WHERE capacity >= 100;
我尝试使用WHERE NOT EXISTS子句,但无法正确使用它。我的目标是排除星期二中午12点预订的房间。
答案 0 :(得分:0)
试试这个:
SELECT
rCode,
capacity
FROM
room
LEFT JOIN
roomBooking ON
room.rCode = roomBooking.roomCode AND
roomBooking.dayReq = 'Tuesday' AND -- fix with the right value
roomBooking.timeReq = '12:00' -- fix with the right value
WHERE
capacity >= 100 AND
roomBooking.BookingID IS NULL -- this will exclude all rooms with bookings on that date/time
答案 1 :(得分:0)
NOT EXISTS是要走的路。您想要选择不存在特定类型的房间的房间:
select *
from room
where capacity >= 100
and not exists
(
select *
from roombooking
where roomBooking.roomcode = room.rcode
and dayreq = ...
and timereq = ...
);
我没有写出日期和时间的标准,因为我不知道这些字段包含哪些字段以及它们是什么数据类型。不知怎的,你将从dayreq获得工作日和timereq的时间。 (也不应该预定房间预定的时间跨度,顺便说一下?)