用外联接
select A.ID, B.ID, A.count, B.count from 
A full outer join B
on A.ID = B.ID查询结果如下:
A.ID, B.ID, A.count, B.count
1001  1001     20       30
1002  null     30       null
null  1003     null     50
 我想将返回如下的查询结果,我要怎么办?
ID,   A.count, B.count
1001  20       30
1002  30       null
1003  null     50

解决方案 »

  1.   

    A,B表的结构一样的。ID COUNT 两个字段。
    A 表数据:
    1001   20  
    1002   30 
    B 表数据
    1001   30  
    1003   50
      

  2.   

    --try
    select [ID]=isnull(A.ID, B.ID), A.count, B.count from 
    A full outer join B
    on A.ID = B.ID
      

  3.   

    create table A([ID] int, [Count] int)
    insert A select  1001,   20  
    union all select 1002,   30 create table B([ID] int, [Count] int)
    insert B select  1001,   30  
    union all select 1003,   50select 
    [ID]=isnull(A.[ID], B.[ID]),
    A_Count=A.[Count],
    B_Count=B.[Count]
    from A
    full join B on A.[ID]=B.[ID]
    order by 1
    --result
    ID          A_Count     B_Count     
    ----------- ----------- ----------- 
    1001        20          30
    1002        30          NULL
    1003        NULL        50(3 row(s) affected)
      

  4.   

    select [ID]=isnull(A.ID, B.ID), A.count, B.count from 
    A full outer join B
    on A.ID = B.ID在oracle9.2中好像不能使用以上语句
      

  5.   

    终于知道了,改称nvl()函数就行了。谢谢marco08(天道酬勤)
      

  6.   

    在SQL Server中,下面的写法应该可以做到:select COALESCE(A.ID, B.ID) as ID, A.count, B.count from 
    A full outer join B
    on A.ID = B.ID