如何从三个表中删除

时间:2015-08-29 20:44:03

标签: sql-server-2008 join sql-delete

房间桌>> room_id,标准

来宾表>> guest_id,访客类型

预订表>> booking_id,room_id,guest_id

我的问题是>删除已预订“豪华”客房的客人的记录。 (豪华是一种标准)

请帮助?查询我是SQL的初学者。

1 个答案:

答案 0 :(得分:0)

我假设您只想从booking表中删除。我还假设您需要从guest表中指定某些,否则您根本不需要第三个表。

DELETE b
FROM booking b
INNER JOIN room r
    ON r.room_id = b.room_id
INNER JOIN guest g
    ON g.guest_id = b.guest_id
WHERE r.standard = 'Deluxe'
    AND g.field = ????

然而,这是MS SQL扩展。 ANSI的方法是:

DELETE FROM booking b
WHERE EXISTS (
        SELECT 1
        FROM room r
        WHERE r.room_id = b.room_id
            AND r.standard = 'Deluxe')
    AND EXISTS (
        SELECT 1
        FROM guest g
        WHERE g.guest_id = b.room_id
            AND g.field = ????)