问题:编写SQL语句以检索从不的客户的名字和姓氏 保留了一个家庭('F')房间。您必须使用子查询。 (结果= 4行)
我的代码:
select firstname,lastname
from guest, reservation
where reservation.guestid=guest.guestid
and roomnum in
(select roomnum
from room
where roomtype = 'f');
我得到26行。 我想我做的子查询错了。
答案 0 :(得分:0)
您应该完全避免使用JOIN,只需使用子查询:
SELECT firstname, lastname
FROM guest
WHERE guestid NOT IN (
SELECT guestid
FROM reservation, room
WHERE reservation.roomnum=room.roomnum
AND roomtype='f'
)
如果他们有多个预订,这可以避免多次获得同一个访客。
答案 1 :(得分:0)
from guest join reservation on reservation.guestid = guest.guestid
。)
关于您的问题:您想从来宾中选择名称。没有加入这里。然后,您要确保客人没有F预订存在。 NOT EXISTS子句或通常更简单:NOT IN子句。
select firstname, lastname
from guest
where guestid not in
(
select guestid
from reservation
where roomnum in
(
select roomnum
from room
where roomtype = 'F'
)
);
答案 2 :(得分:0)
您需要从未进行过家庭类型预订的访客列表(第一个子查询处理它),您还需要删除从未进行过预订的访客(第二个子查询会处理它)。
SELECT firstname, lastname
FROM guest
WHERE guestid NOT IN (
SELECT guestid
FROM reservation, room
WHERE reservation.roomnum=room.roomnum
AND roomtype='f'
) and guestid in
(
SELECT guestid
FROM reservation, room
WHERE reservation.roomnum=room.roomnum
)
查看为试用而创建的SQL Fiddle。 http://sqlfiddle.com/#!9/b4221/2/0