日期:2014-05-18 浏览次数:20585 次
declare @t1 table(期间 int,科目编码 varchar(50),科目名称 varchar(50),c3 money) declare @t2 table(科目编码 varchar(50),科目名称 varchar(50)) insert into @t1 select 1,'1','科目名称1',100.55 union all select 1,'2','科目名称2',120.0 union all select 1,'3','科目名称3',20 union all select 2,'3','科目名称4',54.21 union all select 2,'4','科目名称4',5 insert into @t2 select '1','科目名称1' union all select '2','科目名称2' union all select '3','科目名称3' union all select '4','科目名称4' union all select '5','科目名称5' select * from @t1 select * from @t2 /* 想得到结果: 期间 科目编码 科目名称 c3 1 1 科目名称1 100.55 1 2 科目名称2 120.0 1 3 科目名称3 20 1 4 科目名称4 NULL 1 4 科目名称5 NULL 2 1 科目名称1 NULL 2 2 科目名称2 NULL 2 3 科目名称3 54.21 2 4 科目名称4 5 2 4 科目名称5 NULL */
create table t1 ( a int, b varchar(50), c varchar(50), d money ) insert into t1 select 1,'1','科目名称1',100.55 union all select 1,'2','科目名称2',120.0 union all select 1,'3','科目名称3',20 union all select 2,'4','科目名称4',54.21 union all select 2,'4','科目名称4',5 create table t2 ( b varchar(50), c varchar(50) ) insert into t2 select '1','科目名称1' union all select '2','科目名称2' union all select '3','科目名称3' union all select '4','科目名称4' union all select '5','科目名称5' SELECT t1.a,t3.B,T3.C,t1.d FROM t1 FULL JOIN( SELECT * FROM t2 ) T3 ON t1.B =T3.b AND t1.c=T3.c a B C d ----------- -------------------------------------------------- -------------------------------------------------- --------------------- 1 1 科目名称1 100.55 1 2 科目名称2 120.00 1 3 科目名称3 20.00 2 4 科目名称4 54.21 2 4 科目名称4 5.00 NULL 5 科目名称5 NULL (6 行受影响)