我有这个架构:
Hotel(hotelNo, hotelName, city)
Room (roomNo, hotelNo, type, price)
Booking (hotelNo, guestNo, dateFrom, dateTo, roomNo)
Guest(guestNo, GuestName, GuestAddress)
我需要列出目前入住Grosvenor Hotel酒店的所有客人。这就是我想出的:
SELECT * FROM Guest
WHERE guestNo IN
(SELECT guestNo FROM Booking
WHERE dateFrom <= CURRENT_DATE AND
dateTo >= CURRENT_DATE AND
hotelNo =
(SELECT hotelNo FROM Hotel
WHERE hotelName = ‘Grosvenor Hotel’));
然后,我检查了答案表,该答案表提供了几乎相同的解决方案,而不是IN
使用=
。即
SELECT * FROM Guest
WHERE guestNo =
(SELECT gues....
肯定=
会导致返回单个值,因为子查询会返回一组guestNo,IN
应该是正确的吗?提供的答案是错误的还是我错了?
答案 0 :(得分:2)
当您将一个值与另一个值进行比较时,可以使用相等比较=
。但是,如果要检查值列表或子查询中是否包含值,则必须使用IN
。
还有一件事:使用适当的关系可以更容易地解决这个问题,而不是嵌套查询:
select g.*
from Guest as g
inner join booking as b on g.guestNo = b.guestNo
inner join Hotel as h on b.hotelNo = h.hotelNo
where h.hotelName = 'Grosvenor Hotel'
and dateFrom <= CURRENT_DATE
and dateTo >= CURRENT_DATE;
答案 1 :(得分:1)
WHERE var1 IN ('value1', 'value2')
相当于
WHERE var1 = 'value1' OR var1 = 'value2'