码迷,mamicode.com
首页 > 数据库 > 详细

mysql You can't specify target table for update in FROM clause解决方法

时间:2019-11-15 10:42:01      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:sql语句   HERE   通过   you   mes   upd   sage   group by   table   

mysql You can‘t specify target table for update in FROM clause解决方法
出现这个错误的原因是不能在同一个sql语句中,先select同一个表的某些值,然后再update这个表。

<pre>
mysql> update message set content=‘Hello World‘ where id in(select min(id) from message group by uid);
ERROR 1093 (HY000): You can‘t specify target table ‘message‘ for update in FROM clause
</pre>

因为在同一个sql语句中,先select出message表中每个用户消息的最小id值,然后再更新message表,因此会出现 ERROR 1093 (HY000): You can’t specify target table ‘message’ for update in FROM clause 这个错误。


解决方法:select的结果再通过一个中间表select多一次,就可以避免这个错误
<pre>
update message set content=‘Hello World‘ where id in( select min_id from ( select min(id) as min_id from message group by uid) as a );
</pre>

mysql You can't specify target table for update in FROM clause解决方法

标签:sql语句   HERE   通过   you   mes   upd   sage   group by   table   

原文地址:https://www.cnblogs.com/newmiracle/p/11864844.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!