简体   繁体   English

SQL查询根据来自另一列的数据更新一列

[英]SQL Query to Update One Column Based on Data from Another

I need to update a table column in one table with data from another based on whether a specific id matches. 我需要根据特定的ID是否匹配,用另一个表中的数据更新一个表中的表列。

Basically, I have the following schema: 基本上,我具有以下架构:

TABLE accounts FIELD old_user_id TABLE帐户FIELD_USER_ID

TABLE users FIELD old_user_id FIELD new_user_id TABLE用户FIELD old_user_id FIELD new_user_id

I need to loop through all old_user_id's in the accounts table checking them against the old_user_id field in the users table, and then take the new_user_id value in the users table and replace the old_user_id value in the accounts table. 我需要遍历帐户表中的所有old_user_id,并对照用户表中的old_user_id字段进行检查,然后在用户表中获取new_user_id值,并替换帐户表中的old_user_id值。

Seems like a simple thing to do but as my SQL is not amazing I'm struggling with working this out. 看起来很简单,但是由于我的SQL并不令人惊讶,因此我正在努力解决这一问题。

Try This: 尝试这个:

          UPDATE A 
          SET
                 A.old_user_id = U.new_user_id
          FROM Accounts A
          JOIN   Users U
          ON A.old_user_id = U.old_user_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM