日期:2014-05-16  浏览次数:20425 次

数据库查询多表查询时,补全示例



?生成测试表和数据

create table test1
(
	resId int primary key identity(1,1),
	id1 varchar(20),
	id2 varchar(20),
	id3 varchar(20),
)
go

create table test2
(
	resId int primary key identity(1,1),
	id4 varchar(20),
	id5 varchar(20),
	id6 varchar(20),
)
go

insert into test1 values('11','22','33')
insert into test2 values('44','55','66')
insert into test1 values('444','555','666')
insert into test2 values('444','555','666')
insert into test2 values('444','555','666')
select * from test1
select * from test2

?<!--StartFragment -->?

?

?

执行SQL查询

select t1.resId as 'resId',
id1,id2,id3,id4,id5,id6
from test1 t1
full outer join test2 t2
on t1.resId = t2.resId

?<!--StartFragment -->?

?

select (case when t1.resId is not null then t1.resId when t2.resId is not null then t2.resId else '' end ) as resId,
id1,id2,id3,id4,id5,id6
from test1 t1
full outer join test2 t2
on t1.resId = t2.resId

?<!--StartFragment -->?

?

?