如何更新SQL中同一个表中的数据?

时间:2018-06-04 06:17:27

标签: mysql sql

我想更新提供以下用户名的user_placement_id对应user_placement列。我想使用引用用户名的user_placement_id更新user_placement

user_id | user_name | user_placement | user_placement_id
--------+-----------+----------------+-------------------
1       | jone1     | jone2          | 0
2       | jone2     | jone1          | 0
3       | jone3     | jone2          | 0
4       | jone4     | jone3          | 0

1 个答案:

答案 0 :(得分:0)

我认为你可以在这里使用更新自我加入:

UPDATE yourTable t1
INNER JOIN yourTable t2
    ON t1.user_placement = t2.user_name
SET t1.user_placement_id = t2.user_id;