update ... where a.uid in(select ...) SQL更新字段与条件字段同一个
教程分享
>
Java教程
(1685)
2023-03-28 11:29:14
需求:将表(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
)
https://www.leftso.com/article/852.html