SQL查询根据来自另一列的数据更新一列 [英] SQL Query to Update One Column Based on Data from Another

查看:980
本文介绍了SQL查询根据来自另一列的数据更新一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据特定ID是否匹配,用另一个表中的数据更新一个表中的表列.

I need to update a table column in one table with data from another based on whether a specific id matches.

基本上,我有以下架构:

Basically, I have the following schema:

TABLE帐户 FIELD old_user_id

TABLE accounts FIELD old_user_id

TABLE用户 FIELD old_user_id FIELD new_user_id

TABLE users FIELD old_user_id FIELD new_user_id

我需要遍历accounts表中的所有old_user_id,并对照users表中的old_user_id字段检查它们,然后在users表中获取new_user_id值,并替换accounts表中的old_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.

看起来很简单,但是由于我的SQL并不令人惊讶,我正在努力解决这个问题.

Seems like a simple thing to do but as my SQL is not amazing I'm struggling with working this out.

推荐答案

尝试一下:

          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

这篇关于SQL查询根据来自另一列的数据更新一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆