MySQL - 使用来自相同 table 子查询的值进行更新

MySQL - Update by using a value from sub query on same table

我有一个 table 'mytable',它具有以下结构和示例数据。

+----+------------+--------------------+
| id | name       |   password         |
+----+------------+--------------------+
| 1  | Raj        |   somepwd          |
+----+------------+--------------------+
| 2  | Rao        |   abcdefg          |
+----+------------+--------------------+
| 3  | Uday       |                    |
+----+------------+--------------------+

我想用 Rao 的密码更新 Uday 的密码。 谁能帮我 MySQL 的更新查询来解决这个问题。

提前致谢。

UPDATE mytable 
set 
  password=(SELECT password FROM mytable WHERE name LIKE 'Rao') 
WHERE name LIKE 'Uday'

您需要使用此查询。

update mytable as t1,
(select id,`password` from mytable where name = 'Rao') as t2
set t1.password = t2.password
where t1.id = 3
update mytable set password = (select password from mytable where id=2) where id=3

或者您可以使用自连接

update mytable t1 join mytable t2 on t1.id = 3 set t1.password = t2.password
where t2.id=2

注意:使用 id 而不是其他列

我得到了解决方案,实际上最重要的是所有解决方案都给我错误。

UPDATE mytable as aa, (SELECT password FROM mytable where id =2) as bb SET aa.password=bb.password where aa.id=3;