2011-12-15 68 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的分析