2

下面的查询给了我“#1093 - 您不能在 FROM 子句中指定目标表 'reservation_seats' 进行更新”错误。

DELETE 
FROM reservation_seats 
WHERE id IN ( SELECT id 
              FROM reservation_seats 
              WHERE reservation_id NOT IN ( SELECT id FROM reservation)
            );

表结构:

reservation_seats - id,reservation_id,seat_no
reservation       - id,txn_id,name......
reservation table(id) column is used as the foreign in the reservation_seats table.
(reservation.id = reservation_seats.reservation_id )

你能告诉我为什么会这样以及如何解决吗?

谢谢

4

2 回答 2

4

在 MySQL 中,您不能从您选择的表中删除。尝试

DELETE FROM reservation_seats
WHERE id IN ( 
              select * from 
              (
                   SELECT id FROM reservation_seats
                   WHERE reservation_id NOT IN ( SELECT id FROM reservation)
              )
);

无论如何都要欺骗MySQL。顺便说一句,这可以像这样变得更简单

DELETE FROM reservation_seats
WHERE reservation_id NOT IN ( SELECT id FROM reservation)
于 2012-08-22T06:32:06.610 回答
1

你想删除没有任何保留的座位

DELETE FROM reservation_seats
WHERE  reservation_id NOT IN (SELECT id
                              FROM   reservation);
于 2012-08-22T06:35:11.243 回答