update … where a.uid in(select …) SQL更新字段与条件字段同一个

需求:将表(tableA)里面的字段column1值为33的改为22

错误示例

update tableA set column1='22' 
where uid in (
select uid from tableA where column1='33'
)

错误提示是:ERROR 1093 (HY000): You can't specify target table 'apples' for update in FROM clause. MySQL手册[UPDATE documentation](http://dev.mysql.com/doc/refman/5.0/en/update.html)这下面有说明 : “Currently, you cannot update a table and select from the same table in a subquery.”
在这个例子中,要解决问题也十分简单,但有时候不得不通过查询子句来update目标。好在我们有办法。

正确示例:

update tableA set column1='22'
where uid in (
    select uid from (
        select uid from tableA where column1='33'
    ) tmp
)

 

原创文章,作者:kepupublish,如若转载,请注明出处:https://blog.ytso.com/243832.html

(0)
上一篇 2022年4月11日
下一篇 2022年4月11日

相关推荐

发表回复

登录后才能评论