1

我想从这个基于reserveringsnr 的查询中删除所有这些结果。

(SELECT DISTINCT res.reserveringsnr, datum, begintijd, eindtijd, aantalpersonen, klantnr, betaald, tafr.tafelnr, menr.reserveringsnr, menr.menunr, menr.uniqueid FROM reserveringen as res
    INNER JOIN menus_regel as menr
    ON res.reserveringsnr = menr.reserveringsnr
    INNER JOIN tafels_regel AS tafr
    ON res.reserveringsnr = tafr.reserveringsnr
    WHERE res.reserveringsnr = '21')

但是这样的事情似乎不起作用

DELETE FROM 
(SELECT DISTINCT res.reserveringsnr, datum, begintijd, eindtijd, aantalpersonen, klantnr, betaald, tafr.tafelnr, menr.reserveringsnr, menr.menunr, menr.uniqueid FROM reserveringen as res
    INNER JOIN menus_regel as menr
    ON res.reserveringsnr = menr.reserveringsnr
    INNER JOIN tafels_regel AS tafr
    ON res.reserveringsnr = tafr.reserveringsnr
    WHERE res.reserveringsnr = '21')Delete
WHERE Delete.reserveringsnr = '21'

任何人都可以将我推向正确的方向吗?提前致谢!

4

2 回答 2

1

下面的查询将只删除表上的所有记录reserveringen

DELETE  res
FROM    reserveringen as res
        INNER JOIN menus_regel as menr
            ON res.reserveringsnr = menr.reserveringsnr
        INNER JOIN tafels_regel AS tafr
            ON res.reserveringsnr = tafr.reserveringsnr
WHERE   res.reserveringsnr = '21'

但是如果要从所有reserveringsnr匹配的表中删除记录,则需要在DELETE子句中指定所有表名

DELETE  res, menr, tafr
FROM    reserveringen as res
        INNER JOIN menus_regel as menr
            ON res.reserveringsnr = menr.reserveringsnr
        INNER JOIN tafels_regel AS tafr
            ON res.reserveringsnr = tafr.reserveringsnr
WHERE   res.reserveringsnr = '21'
于 2013-01-14T15:51:05.943 回答
1

如果您必须在一行中完成,那么@JW 的答案看起来是正确的。但是,我更喜欢在单独的行中运行这些以解决性能问题——如果每个表中有很多记录,则连接将比单个删除花费更长的时间。

DELETE 
FROM reserveringen  
WHERE reserveringsnr = '21';

DELETE 
FROM menus_regel 
WHERE reserveringsnr = '21';

DELETE 
FROM tafels_regel 
WHERE reserveringsnr = '21'

这是一个关于删除的有用链接:

http://dev.mysql.com/doc/refman/5.0/en/delete.html

于 2013-01-14T15:58:16.223 回答