0

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

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

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

提前致谢。

4

4 回答 4

0
UPDATE mytable 
set 
  password=(SELECT password FROM mytable WHERE name LIKE 'Rao') 
WHERE name LIKE 'Uday'
于 2017-07-19T12:36:01.897 回答
0

您需要使用此查询。

update mytable as t1,
(select id,`password` from mytable where name = 'Rao') as t2
set t1.password = t2.password
where t1.id = 3
于 2017-07-19T12:48:31.270 回答
0
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 代替其他列

于 2017-07-19T12:58:58.510 回答
0

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

UPDATE mytable as aa, (SELECT password FROM mytable where id =2) as bb SET aa.password=bb.password where aa.id=3;
于 2017-07-19T13:08:23.807 回答