求一条sql语句,关于外链表的
declare @t1 table(期间 int,科目编码 varchar(50),科目名称 varchar(50),c3 money)declare @t2 table(科目编码 varchar(50),科目名称 varchar(50))insert into @t1select 1,'1','科目名称1',100.55 union allselect 1,'2','科目名称2',120.0 union allselect 1,'3','科目名称3',20 union allselect 2,'3','科目名称4',54.21 union allselect 2,'4','科目名称4',5 insert into @t2select '1','科目名称1' union allselect '2','科目名称2' union allselect '3','科目名称3' union allselect '4','科目名称4' union allselect '5','科目名称5' select * from @t1select * from @t2/*想得到结果:期间 科目编码 科目名称 c31 1 科目名称1 100.551 2 科目名称2 120.01 3 科目名称3 201 4 科目名称4 NULL1 4 科目名称5 NULL2 1 科目名称1 NULL2 2 科目名称2 NULL2 3 科目名称3 54.212 4 科目名称4 52 4 科目名称5 NULL*/
create table t1(a int,b varchar(50),c varchar(50),d money)insert into t1select 1,'1','科目名称1',100.55 union allselect 1,'2','科目名称2',120.0 union allselect 1,'3','科目名称3',20 union allselect 2,'4','科目名称4',54.21 union allselect 2,'4','科目名称4',5 create table t2(b varchar(50),c varchar(50))insert into t2select '1','科目名称1' union allselect '2','科目名称2' union allselect '3','科目名称3' union allselect '4','科目名称4' union allselect '5','科目名称5'SELECT t1.a,t3.B,T3.C,t1.d FROM t1 FULL JOIN( SELECT * FROM t2 ) T3ON t1.B =T3.b AND t1.c=T3.ca B C d----------- -------------------------------------------------- -------------------------------------------------- ---------------------1 1 科目名称1 100.551 2 科目名称2 120.001 3 科目名称3 20.002 4 科目名称4 54.212 4 科目名称4 5.00NULL 5 科目名称5 NULL(6 行受影响)