0

Possible Duplicate:
SQL Delete: can't specify target table for update in FROM clause

I'm trying to delete some rows, but is currently not in success.

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

You can't specify target table 'product_pictures' for update in FROM clause

I've never seen this error message before, nor has I been able to find some useful info about what I'm doing wrong.

Example of rows:

ID    Picture
19    picture-grey.jpg
20    picture-grey.jpg
21    picture-grey.jpg
Community
  • 1
  • 1
Kristian
  • 1,343
  • 4
  • 29
  • 47

3 Answers3

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

This cheat will fool mysql analyzer

zerkms
  • 249,484
  • 69
  • 436
  • 539
4
DELETE a 
FROM product_pictures AS a
  JOIN product_pictures AS b
    ON b.picture = a.picture
WHERE b.id = ?

or:

DELETE a 
FROM product_pictures AS a
  JOIN 
    ( SELECT DISTINCT picture
      FROM product_pictures
      WHERE id = ?
    ) AS b
    ON b.picture = a.picture
ypercubeᵀᴹ
  • 113,259
  • 19
  • 174
  • 235
1

Your query has a loop in it. Why don't you just do

DELETE FROM product_pictures
WHERE id = ?
jzila
  • 724
  • 4
  • 11