以前遇到过几次这个问题,都觉得问题不大,所以没有记录,但是这次又遇到了,而且没有第一时间想起来,所以还是有记录下的必要

MySQL    delete语句使用子查询操作同一张表的时候会抛出

DELETE from abc where id in (select c.id from abc c where c.C_ID is null )

 

[Err] 1093 - You can't specify target table 'abc' for update in FROM clause

解决办法:加上一个虚拟中间表中转一下

DELETE from abc where id in (select * from (select c.ID from abc c where c.C_ID is null) s  );

在更新的时候也会存在同样的问题

解决到了,但是原理是什么?不清楚,希望有大神看到后指教下,谢谢。

还有一个问题,删除的时候是不能起别名的

delete from abc s where s.ID = 'qeq';
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's where s.ID = 'qeq'' at line 1

别名拿掉就可以正常执行了