sqlserver 数据库表数据合并

2026年09月26日 09:10
有2个网友回答
网友(1):

--先把表1和表2的solver合并起来,再分别和表1,表2做连接
With T
As
(
    Select solver From 表1
    union 
    Select solver From 表2
)
Select T.solver,Isnull(A.amount1,0) As amount1,isnull(B.amount2,0) As amount2 From T
Left Join 表1 A on T.solver=A.solver
Left Join 表2 B on T.solver=B.solver

网友(2):

先建好表三,然后执行sql语句:
insert into table3 values(select t1.solver,t1.amount1,t2.amount2 from table1 t1 inner join table2 t2 on t1.solver = t2.solver);
insert into table3 values(select t1.solver,t1.amount1,0 from table1 t1,table t2 where t1.solver <> t2.solver);
insert into table3 values(select t2.solver,0,t2.amount1 from table1 t1,table t2 where t1.solver <> t2.solver);