22,210
社区成员
发帖
与我相关
我的任务
分享
create table a(date datetime,hour int)
insert a
select '2008/01/02', 8
union all select '2008/01/03', 9
union all select '2008/01/04', 8
create table b(date datetime,overtime int)
insert b
select '2008/01/01', 2
union all select '2008/01/02', 3
select isnull(a.date,b.date) date,a.hour,b.overtime
from a full join b
on a.date=b.date
order by isnull(a.date,b.date)
drop table a,b
/*
name name
----------- ----------- -----------
1 3 NULL
2 4 8
3 5 NULL
4 6 NULL
0 NULL 7
*/
declare @ta table(id int,name int)
insert @ta select
1,3 union select
2,4 union select
3,5 union select
4,6
declare @tb table(id int,name int)
insert @tb select
0,7 union select
2,8
select isnull(a.id,b.id),a.name, b.name
from @ta a full join @tb b on a.id = b.id
select isnull(a.date,b.date) date,a.hour,b.overtime
from a full join b
on a.date=b.date
declare @t table(id int)
insert into @t values(1)
insert into @t values(2)
insert into @t values(3)
insert into @t values(4)
declare @b table(id int)
insert into @b values(0)
insert into @b values(2)
select isnull(a.id,b.id) id from @t a full join @b b on a.id=b.id order by id
declare @t table(id int)
insert into @t values(1)
insert into @t values(2)
insert into @t values(3)
insert into @t values(4)
declare @b table(id int)
insert into @b values(0)
insert into @b values(2)
select * from (
select * from @t
union all
select * from @b where id not in (select id from @t)
) t
order by id
select id from a
union
select id from b
--> 测试数据: #A
if object_id('tempdb.dbo.#A') is not null drop table #A
create table #A (ID int)
insert into #A
select 1 union all
select 2 union all
select 3 union all
select 4
--> 测试数据: #B
if object_id('tempdb.dbo.#B') is not null drop table #B
create table #B (ID int)
insert into #B
select 0 union all
select 2
select ID from #A
UNION
select ID from #B
/*
ID
-----------
0
1
2
3
4
*/