Mysql delete with subquery [duplicate]

橙三吉。 提交于 2019-12-23 04:46:03

问题


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

回答1:


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



回答2:


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

This cheat will fool mysql analyzer




回答3:


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

DELETE FROM product_pictures
WHERE id = ?


来源:https://stackoverflow.com/questions/8527569/mysql-delete-with-subquery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!