Mysql: Table 'name' is specified twice, both as a target for 'UPDATE' and as a separate source for data

mysql在执行删除、更新的时候,可能会出现这样的错误,比如你写了类似下面的sql:

UPDATE MYTABLE SET COL=COL+1 WHERE ID IN (SELECT ID FROM MYTABLE WHERE OTHERCOL=0)

or

DELETE FROM MYTABLE WHERE ID IN (SELECT ID FROM MYTABLE WHERE OTHERCOL=0)

这个时候就会出现Table 'xxx' is specified twice这样的错误,不知道mysql为什么会做这样的冻结,知道的大神请给个链接。

要解决这个问题很简单,既然table 'xxx'不能放到子查询里面,那就做一个缓存表呗。

DELETE FROM MYTABLE WHERE ID IN (SELECT * FROM (SELECT ID FROM MYTABLE WHERE OTHERCOL=0) AS TEMP)

 这样就没有问题了。

posted on 2015-04-02 17:09  paul liu  阅读(9161)  评论(0编辑  收藏  举报

导航