Hotel( hotelNo, hotelName, hotelAddress, country)
Room (roomNo, hotelNo, type, price)
Guest( guestNo, guestName, guestAddress, country)
Booking (hotelNo, guestNo, dateFrom, dateTo, roomNo)
我要解决的问题是
-使用2019-02-05作为今天的日期。列出名称中带有“格罗夫纳”的酒店的所有客房的详细信息,包括入住该房间的客人的姓名(如果房间被占用的话)。在酒店编号中列出,房间编号为无订单。
它运行,但是显示“未选择行”。 我可能会错过什么?
SELECT r.roomNo, r.hotelNo, r.type, r.price, guestName
FROM Room r
LEFT OUTER JOIN Booking b
ON r.hotelNo = b.hotelNo
NATURAL JOIN Guest
WHERE r.hotelNo IN
(SELECT hotelNo FROM Hotel
WHERE hotelName LIKE 'Grosvener%')
AND ((b.dateFrom <= DATE'2019-02-05') AND
(dateTo IS NULL OR dateTo >= DATE'2019-02-05'))
ORDER BY hotelNo, roomNo;
答案 0 :(得分:1)
我认为您也必须在Guest表中使用标识符。请尝试以下查询。
SELECT r.roomNo, r.hotelNo, r.type, r.price, g.guestName
FROM Room r
LEFT OUTER JOIN Booking b
ON r.hotelNo = b.hotelNo
NATURAL JOIN Guest g
WHERE r.hotelNo IN
(SELECT hotelNo FROM Hotel
WHERE hotelName LIKE 'Grosvener%')
AND ((b.dateFrom <= DATE'2019-02-05') AND
(dateTo IS NULL OR dateTo >= DATE'2019-02-05'))
ORDER BY r.hotelNo, r.roomNo;
答案 1 :(得分:1)
为什么要使用自然联接?使用左外部联接(如果指定预订没有任何客人)。
SELECT r.roomNo, r.hotelNo, r.type, r.price, guestName
FROM Room r
LEFT OUTER JOIN Booking b
ON r.hotelNo = b.hotelNo
LEFT OUTER JOIN Guest g
ON b.guestNo=g.guestNo
WHERE r.hotelNo IN
(SELECT hotelNo FROM Hotel
WHERE hotelName LIKE 'Grosvener%')
AND ((b.dateFrom <= DATE'2019-02-05') AND
(dateTo IS NULL OR dateTo >= DATE'2019-02-05'))
ORDER BY hotelNo, roomNo;
答案 2 :(得分:1)
我想以SELECT hotelNo ...
开头的子查询的括号用法存在问题,以下用法可能有帮助:
SELECT r.roomNo, r.hotelNo, r.type, r.price, guestName
FROM Room r
LEFT OUTER JOIN Booking b
ON r.hotelNo = b.hotelNo NATURAL
JOIN Guest
WHERE r.hotelNo IN
(SELECT hotelNo
FROM Hotel
WHERE hotelName LIKE 'Grosvener%'
AND ( ( b.dateFrom <= date'2019-02-05' AND dateTo >= date'2019-02-05' )
OR ( dateTo IS NULL )))
ORDER BY hotelNo, roomNo;
答案 3 :(得分:0)
谢谢大家回答我的问题。
SELECT r.roomNo, r.hotelNo, r.type, r.price, guestName
FROM Hotel h
JOIN Room r
ON r.hotelNo = h.hotelNo
LEFT JOIN
(
SELECT b.hotelNo, b.roomNo, g.guestName, b.dateTo, b.dateFrom
FROM Guest g
JOIN Booking b
ON g.guestNo = b.guestNo
WHERE b.dateFrom <= DATE'2019-02-05'
AND (dateTo IS NULL OR dateTo >= DATE'2019-02-05')
) bo
ON r.hotelNo = bo.hotelNo
AND r.roomNo = bo.roomNo
WHERE h.hotelName LIKE '%Grosvenor%'
ORDER BY h.hotelNo, r.roomNo
;
您如何看待这段代码?这似乎是给我的唯一代码 正确答案,但时间太长。