[mysql] update……from……

今天插入一条数据就像这样

update cgs set isleaf=(select if((select id from cgs where pd=0 and sd=5 limit 1), 0, 1)) where id=0

然后报错:

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

不能select同一个表中的某些值,然后update到同一个表中

然后修改如下:

update cgs set isleaf=(select if((select a.id from (select * from cgs where pd=0 and sd=5 limit 1 )a ), 0, 1)) where id=0

重点在  select a.id from (select……)a

参考资料:

http://www.cnblogs.com/chy1000/archive/2010/03/02/1676282.html

https://stackoverflow.com/questions/45494/mysql-error-1093-cant-specify-target-table-for-update-in-from-clause

posted @ 2017-08-28 15:32  wilderness  阅读(356)  评论(0编辑  收藏  举报