Sql 如何将列数据复制到两个表上都存在公共列数据的另一个表中?
我想将两列的数据复制到另一个表中,该表有许多列,并且两个表上都有一个公共列 这是我的桌子:Sql 如何将列数据复制到两个表上都存在公共列数据的另一个表中?,sql,sql-server,Sql,Sql Server,我想将两列的数据复制到另一个表中,该表有许多列,并且两个表上都有一个公共列 这是我的桌子: 表1 ID | col1 | col2 1 | 13 | 12 2 | 8 | 3 3 | 7 | 10 表2 ID | col1 | col2 | col3 | col4 1 | 0 | 0 | 0 | 0 2 | 0 | 0 | 0 | 0 3 | 0 | 0 | 0
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'a'.
无论如何,有可能做到吗?
很抱歉,如果我没有用一种很好的方式编写表,但是stackoverflow似乎没有创建表。您的语法对于MySQL来说看起来是正确的,但最好是这样编写:
UPDATE table2 a join
table1 b
on a.ID = b.ID
SET a.col1 = b.col1,
a.col2 = b.col2,
a.col3 = a.col3 + b.col1,
a.col4 = a.col4 + b.col2;
对于SQL Server,语法为:
UPDATE a
SET col1 = b.col1,
col2 = b.col2,
col3 = a.col3 + b.col1,
col4 = a.col4 + b.col2
FROM table2 a join
table1 b
on a.ID = b.ID;
SQL Server中的错误可能在表后面的别名上,或者在
set
语句中的a
上。mysql或SQL Server?你给两个都加了标签
UPDATE a
SET col1 = b.col1,
col2 = b.col2,
col3 = a.col3 + b.col1,
col4 = a.col4 + b.col2
FROM table2 a join
table1 b
on a.ID = b.ID;