设表A为:
ID Count
5  25
8  11
12 15表B为:
ID Count
2  8
5  23
15 18那么如何得到下表呢? 好像单union或full join不行.
ID Count_A Count_B
2  Null    8
5  25      23
8  11      Null
12 15      Null
15 Null    18

解决方案 »

  1.   

    select a.ID, 表A.Count Count_A,表B.Count Count_B 
    from (select ID from 表A union select ID from 表B) a
    left join 表A on a.ID=表A.ID
    left join 表B on a.ID=表B.ID没测试,大概是这样的。
      

  2.   

    select a.ID, 表A.Count as Count_A,表B.Count as Count_B 
    from (select ID from 表A union select ID from 表B) as a
    left join 表A on a.ID=表A.ID
    left join 表B on a.ID=表B.ID不需要加上as吗?
      

  3.   

    create table a(id int,[count] int)
    insert into a select 5,25
    union all select 8,11
    union all select 12,15
    create table b(id int,[count] int)
    insert into b select 2,8
    union all select 5,23
    union all select 15,18
    select a.id,[count] as count_a,
    count_b =(case when exists(select 1 from b where a.id=b.id) then (select [count] from b where a.id=b.id) 
    else Null end)  from a
    union
    select b.id,count_a =(case when exists(select 1 from a where a.id=b.id) then (select [count] from a where a.id=b.id) 
    else Null end),[count] as count_b
    from b