2013-01-14 89 views
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' 

任何人都可以把我推了一下方向是正確的? 在此先感謝!

+1

當你說你想「*刪除所有這些結果*」,你想刪除所有的相關記錄底層表還是僅來自其中的一些? – eggyal

+0

'從wheretable條件中刪除(你選擇那裏)'?請注意,在MySQL中,您無法從您正在從中進行選擇的位置更新/刪除。 –

+0

一般而言,您只能從一張表中一次從子表開始刪除。 – HLGEM

回答

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' 
+0

謝謝,作品像魅力! –

+0

使用下面的第二個查詢。 –

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