2011-12-15 69 views
0

Possible Duplicate:
SQL Delete: can't specify target table for update in FROM clausemysql的刪除與子查詢

我想刪除一些行,但目前沒有成功。

DELETE FROM product_pictures 
WHERE picture = (SELECT picture FROM product_pictures WHERE id = ?) 

不能在FROM子句指定目標表「product_pictures」的更新

我以前從來沒見過此錯誤信息,也沒有我能找到什麼我一些有用的信息做錯了。行

例子:

ID Picture 
19 picture-grey.jpg 
20 picture-grey.jpg 
21 picture-grey.jpg 
+0

請您確認,如果你只是想刪除行與給定的ID或者如果你有更復雜的要求。 – 2011-12-15 22:40:57

+0

@kris:或者你想刪除所有具有相同`圖片`的行'id =?' – 2011-12-15 22:42:54

+0

@ypercube - 是 – Kristian 2011-12-15 22:43:34

回答

3
DELETE a 
FROM product_pictures AS a 
    JOIN product_pictures AS b 
    ON b.picture = a.picture 
WHERE b.id = ? 

或:

DELETE a 
FROM product_pictures AS a 
    JOIN 
    (SELECT DISTINCT picture 
     FROM product_pictures 
     WHERE id = ? 
    ) AS b 
    ON b.picture = a.picture 
1

你的查詢有一個循環,它。你爲什麼不只是做

DELETE FROM product_pictures 
WHERE id = ? 
9
DELETE FROM product_pictures 
WHERE picture = (SELECT picture FROM (SELECT picture FROM product_pictures WHERE id = ?) x) 

這種作弊會愚弄MySQL的分析