日期:2014-05-18  浏览次数:20585 次

求一条sql语句,关于外链表的
SQL code

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 * from t1
select * from t2

select b1.a as '期间',b1.b as '科目编码',b1.c as '科目名称',t1.d as 'c3' from 
(select a1.a as a,t2.* from t2 cross join (select distinct a from t1) as a1) as b1 left join t1 
on b1.a=t1.a and b1.b=t1.b and b1.c=t1.c

---------------------------------
期间 科目编码 科目名称 c3
1 1 科目名称1 100.55
1 2 科目名称2 120.00
1 3 科目名称3 20.00
1 4 科目名称4 NULL
1 5 科目名称5 NULL
2 1 科目名称1 NULL
2 2 科目名称2 NULL
2 3 科目名称3 NULL
2 4 科目名称4 54.21
2 4 科目名称4 5.00
2 5 科目名称5 NULL
------解决方案--------------------
SQL code

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 行受影响)