mysql 从同一个表获取数据update自身报错一例

我在mysql中,想将一些记录的某个字段,改成跟某条记录的该字段的值一样,语句如下:

sql 复制代码
update org_user set password=(select password from org_user where loginname='admin' limit 1) where userid>1000;

结果就报错了,提示:

Error Code: 1093. You can't specify target table 'org_user' for update in FROM clause。看这意思,不能从表中获取数据来更新自己?

果然,如果从别的表拿数据就可以:

sql 复制代码
--克隆一个表
create table org_user_tmp as select * from org_user;

--从正式表拿数据更新克隆表,OK
update org_user_tmp set password=(select password from org_user where loginname='admin' limit 1) where userid>1000;

这种语句在别的数据库,比如oracle,是没有问题的。来到mysql就不行了。后来改成这样就可以了:

sql 复制代码
update org_user as u1 
join(select password from org_user where loginname='admin' limit 1) as u2
set u1.PASSWORD=u2.password
where u1.userid>1000;

怪哉。

相关推荐
程序新视界23 分钟前
什么是OLTP ,MySQL是如何支持OLTP的?
数据库·后端·mysql
Lisonseekpan2 小时前
为什么要避免使用 `SELECT *`?
java·数据库·后端·sql·mysql·oracle
Wilson Chen2 小时前
深入理解 MySQL 事务与锁机制:从 ACID 到 Next-Key Lock 的实证之旅
java·数据库·mysql
无敌的牛3 小时前
MySQL的开始,MySQL的安装
数据库·mysql
Zxxxxxy_3 小时前
【MYSQL】增删改查
java·数据库·mysql
木辰風3 小时前
如何在MySQL中搜索JSON数据,并去除引号
数据库·mysql·json
计算机学姐4 小时前
基于SpringBoo+Vue的医院预约挂号管理系统【个性化推荐算法+可视化统计】
java·vue.js·spring boot·mysql·intellij-idea·mybatis·推荐算法
计算机学姐4 小时前
基于微信小程序的奶茶店点餐平台【2026最新】
java·vue.js·spring boot·mysql·微信小程序·小程序·mybatis
半路_出家ren6 小时前
图书销售系统数据库设计方案
数据库·mysql·子查询·ddl·dml·数据库设计·分组查询
ayaya_mana6 小时前
MySQL忘记Root密码,详细找回密码步骤
数据库·mysql·adb