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

sql取合集,怎么一句sql解决啊
表A
ID A1
1 a1
2 a2
3 a3

表B
ID B1
3 b3
4 b4
5 b5


想要结果
ID A1 B1
1 a1 NULL
2 a2 NULL
3 a3 b3
4 NULL b4
5 NULL b5
如何查询啊?

------解决方案--------------------
SQL code

declare @表A table (ID int,A1 varchar(2))
insert into @表A
select 1,'a1' union all
select 2,'a2' union all
select 3,'a3'

declare @表B table (ID int,B1 varchar(2))
insert into @表B
select 3,'b3' union all
select 4,'b4' union all
select 5,'b5'

select isnull(a.ID,b.ID) as ID,a.A1,b.B1 from @表A a 
full join @表B b on a.ID=b.ID

/*
ID          A1   B1
----------- ---- ----
1           a1   NULL
2           a2   NULL
3           a3   b3
4           NULL b4
5           NULL b5
*/