select * from (
select id,fname from 表a inner join 表b on 表a.ID!=表b.ID
union all
select id,o.fname+p.fname from 表a o inner join 表b p on o.ID=p.ID
) as a
select isnull(a.ID,b.id) ID ,isnull(fname,'') fname
,isnull(lname,'') lname
,isnull(age,0) age
,isnull(tele,'')tele
from @a a
full outer join @b b on a.id =b.id
order by ID
ID fname lname age tele
----------- -------- -------- ----------- -----------
1 zhu liu 20 32425215
2 ma ling 0 0
3 wang hua 23 543242
5 21 543543543
create table 表a(
Id int,
fname varchar(10),
lname varchar(10)
)
insert 表a
select 1,'zhu','liu' union all
select 2,'ma','ling' union all
select 3,'wang','hua'
create table 表b(
Id int,
age int,
tele varchar(10)
)
insert 表b
select 1,20,'32425215' union all
select 3,23,'543242' union all
select 5,21,'543543543'
select isnull(a.Id,b.Id)as Id,isnull(a.fname,'')as fname ,
isnull(a.lname,'')as lname ,isnull(b.age,'')as age ,isnull(b.tele,'')as tele
from 表a a
full join 表b b
on a.Id=b.Id order by Id
--result:
id fname lname age tele
------------------------------------------
1 zhu liu 20 32425215
2 ma ling 0
3 wang hua 23 543242
5 21 543543543
多写了一句,这样就可以:
select a.id,fname,lname,age,tele from @a a left join @b b on a.id=b.id
union select b.id,fname,lname,age,tele from @a a right join @b b on a.id=b.id
复人:xiaoku(野蛮人(^v^)) ( 一星(中级)) 信誉:100 2007-04-05 16:25:05 得分:0
?
select ID ,isnull(fname,'') fname
,isnull(lname,'') lname
,isnull(age,0) age
,isnull(tele,'')tele
from a
full outer join b on a.id =b.id
------------------
full join有点小问题
declare @a table(Id int,fname varchar(8),lname varchar(8))
insert into @a
select 1,'zhu','liu'
union select 2,'ma','ling'
union select 3,'wang','hua'
declare @b table(Id int,age int,tele int)
insert into @b
select 1,20,32425215
union select 3,23,543242
union select 5,21,543543543
select * from @a;
select * from @b;
select a.id,fname,lname,age,tele from @a a inner join @b b on a.id=b.id
union select a.id,fname,lname,age,tele from @a a left join @b b on a.id=b.id
union select b.id,fname,lname,age,tele from @a a right join @b b on a.id=b.id