SQL 内连接 2 个具有多列条件的表并更新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3156230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 06:44:12  来源:igfitidea点击:

SQL Inner join 2 tables with multiple column conditions and update

sqlsql-updateinner-join

提问by marilyn

I am using this script, trying to join 2 tables with 3 conditions and update T1:

我正在使用此脚本,尝试加入具有 3 个条件的 2 个表并更新 T1:

Update T1 set T1.Inci = T2.Inci 
ON T1.Brands = T2.Brands 
AND T1.Category= T2.Category
AND T1.Date = T2.Date

but I encounter:

但我遇到:

Incorrect syntax near the keyword 'ON'.

Incorrect syntax near the keyword 'ON'.

Can't figure it out why.

想不通为什么。

回答by Robin Day

UPDATE
    T1
SET
    T1.Inci = T2.Inci 
FROM
    T1
INNER JOIN
    T2
ON
    T1.Brands = T2.Brands
AND
    T1.Category= T2.Category
AND
    T1.Date = T2.Date

回答by Bruno Costa

You need to do

你需要做

Update table_xpto
set column_xpto = x.xpto_New
    ,column2 = x.column2New
from table_xpto xpto
   inner join table_xptoNew xptoNew ON xpto.bla = xptoNew.Bla
where <clause where>

If you need a better answer, you can give us more information :)

如果您需要更好的答案,可以给我们更多信息:)

回答by logsvikas

UPDATE T1,T2 
INNER JOIN T1 ON  T1.Brands = T2.Brands
SET 
T1.Inci = T2.Inci
WHERE
    T1.Category= T2.Category
AND
    T1.Date = T2.Date

回答by Yuresh Karunanayake

You should join T1 and T2 tables using sql joins in order to analyze from two tables. Link for learn joins : https://www.w3schools.com/sql/sql_join.asp

您应该使用 sql 连接连接 T1 和 T2 表,以便从两个表中进行分析。学习加入链接:https: //www.w3schools.com/sql/sql_join.asp