更新一个表中具有不同值的多个行

时间:2022-06-20 20:10:30

I have the following 3 tables. The 1st table has about 100 rows. How can I update the CompanyId in the first table with its corresponding value from the 3rd table via one update statement?

我有以下3个表。第一个表有大约100行。如何通过一个更新语句更新第一个表中的CompanyId以及第三个表中的相应值?

T1
-----------------------------
FileId | CompanyId
-----------------------------
ABC      | NULL  
DEF      | NULL

T2
-----------------------------
FileId  | UserId
-----------------------------
ABC       | 123  
DEF       | 456

T3
-----------------------------
UserId  | CompanyId
-----------------------------
123       | V1  
456       | V2

1 个解决方案

#1


3  

Use this statement:

使用此声明:

UPDATE T1
SET COMPAYID = T3.CompanyId
FROM T1 INNER JOIN T2 ON T1.FILEID = T2.FILEID
        INNER JOIN T3 ON T2.USERID = T3.USERID

#1


3  

Use this statement:

使用此声明:

UPDATE T1
SET COMPAYID = T3.CompanyId
FROM T1 INNER JOIN T2 ON T1.FILEID = T2.FILEID
        INNER JOIN T3 ON T2.USERID = T3.USERID