问题
I ran this in mysql client shell on windows. I don't understand what the problem is. I know delete from PageInfo where id
is correct. I know the subquery is correct. I think in is correct but I don't use it that often. This entire thing looks correct but i get a problem somewhere. I don't understand the error message.
How do i delete all the ids the subquery returns?
mysql> delete from PageInfo where id in ( select max(id) from PageInfo where pid
>=2758000 AND pid<2758100 group by pid having count(pid)>1 );
ERROR 1093 (HY000): You can't specify target table 'PageInfo' for update in FROM
clause
回答1:
In MySQL you cannot modify the same table which is a part of subquery.
more info at http://dev.mysql.com/doc/refman/5.6/en/update.html
Workaround for your problem is described here.
回答2:
You could do it like this
delete from PageInfo where id = ( SELECT maxid FROM ( select max(id) as maxid from PageInfo where pid >=2758000 AND pid<2758100 group by pid having count(pid)>1) as tmp)
回答3:
from mysql website. selecting and modifying from same table is not allowed as mentioned
This error occurs in cases such as the following, which attempts to modify a table and select from the same table in the subquery
Incorrectly used table in subquery:
Error 1093 (ER_UPDATE_TABLE_USED)
SQLSTATE = HY000
Message = "You can't specify target table 'x'
for update in FROM clause"
:
来源:https://stackoverflow.com/questions/12994768/delete-from-where-id-in-subquery-error