简体   繁体   English

根据另一个表的行更新表的行

[英]Update Table's rows based on another table'rows

I have 2 tables SQL Server(Tab1,Tab2)我有 2 个表 SQL Server(Tab1,Tab2)

Tab1 has表1有

col1
col2 
col3 

Tab2 has Tab2 有

col11
col22 
col33

I want to copy data form我要复制数据表格

col1 into col11, and col2 into col22. col1 到 col11,col2 到 col22。

if col3 equal to col33.如果 col3 等于 col33。

How can i do that?我怎样才能做到这一点?

Just Join the tables Based on col3 = col33 and Update the other columns.只需加入基于 col3 = col33 的表并更新其他列。

UPDATE T2
    SET
       col11 = T1.col1,
       col22 = T1.col2
    FROM Tab1 T1
       INNER JOIN Tab2 T2
          ON T2.col33 = T1.col3

You have to just select column values from Tab2 and insert in Tab1 as below:您只需从 Tab2 中选择列值并插入 Tab1,如下所示:

INSERT INTO Tab2 (col11,col22,col33) 
SELECT T1.col1,T1.col2,T1.col3 
FROM Tab1 T1
WHERE T1.col3 EXISTS IN(SELECT col33 FROM Tab2 WHERE col33=T1.col3)

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

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