Sql 将多个表的多列添加到一列中
我有两个具有相同列名的表,我必须为这两个表上的某些特定条件添加oprId列值 表1Sql 将多个表的多列添加到一列中,sql,sql-server-2008,Sql,Sql Server 2008,我有两个具有相同列名的表,我必须为这两个表上的某些特定条件添加oprId列值 表1 something oprId abc 1 qwe 2 表2 something oprId abc 2 qwe 5 结果应该是 oprId 3 7 结果: oprId ------ 3 7 我认为你遗漏了问题的重要部分。3和7从何而来?它应该是oprId 1+2和2+5Ok的和。那么您将xyz加入abc和ghj加入qwe?
something oprId
abc 1
qwe 2
表2
something oprId
abc 2
qwe 5
结果应该是
oprId
3
7
结果:
oprId
------
3
7
我认为你遗漏了问题的重要部分。3和7从何而来?它应该是oprId 1+2和2+5Ok的和。那么您将
xyz
加入abc
和ghj
加入qwe
?这些值是查询的参数吗?对不起,我现在已经编辑了这个问题
declare @T1 table (something varchar(3), oprId int)
declare @T2 table (something varchar(3), oprId int)
insert into @T1 values ('abc', 1),('qwe', 2)
insert into @T2 values ('abc', 2),('qwe', 5)
select T1.oprId+T2.oprId as oprId
from @T1 as T1
inner join @T2 as T2
on T1.something = T2.something
oprId
------
3
7