将多个表的多列添加到一列中

时间:2011-10-20 17:33:04

标签: sql sql-server-2008

我有两个具有相同列名的表我必须在两个表上为某些特定条件添加oprId列值。

表1

something  oprId

abc          1
qwe          2

表2

something  oprId

abc          2
qwe          5 

结果应为

oprId
3
7

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

SELECT ISNULL(A.something,B.something) Something,
       ISNULL(A.oprId,0)ÍSNULL(B.oprId,0) oprId
FROM Table1 A
FULL JOIN Table2 B
ON A.something = B.something