有如下的两张表:
table_cn:
name  count
a       1
b       3
c       2table_hk:
name count
a      2
b      5
d      3怎么得出这样的结果:
name count_cn   count_hk
a      1          2
b      3          5
c      2          0
d      0          3数据库是oracle,请高手写一下,谢谢,最好能按count_cn+count_hk的总数从高到低排序。
谢谢,在线等...

解决方案 »

  1.   


    select nvl(a.name,b.name),nvl(a.count,0) count_cn,nvl(b.count,0) count_hk from table_cn a,table_hk b
    where a.name=b.name(+)
      

  2.   

    select a.name,nvl(a.count,0) count_cn,nvl(b.count,0) count_hk from table_cn a,table_hk b where
    a.name = b.name(+)
    union
    select b.name,nvl(a.count,0) count_cn,nvl(b.count,0) count_hk from table_cn a,table_hk b where
    a.name(+) = b.name;
    /
      

  3.   

    select nvl(a.name, b.name), a.count nvl(count_cn,0), nvl(b.count,0) count_hk from table_cn a full join table_hk b on a.name=b.name;
      

  4.   

    按count_cn+count_hk的总数从高到低排序,总数相等按count_cn、count_hk从高到低排序
    select name,count_cn,count_hk from 
    (select a.name name,nvl(a.count,0) count_cn,nvl(b.count,0) count_hk,nvl(a.count,0)+nvl(b.count,0) from table_cn a,table_hk b where
    a.name = b.name(+)
    union
    select b.name name,nvl(a.count,0) count_cn,nvl(b.count,0) count_hk,nvl(a.count,0)+nvl(b.count,0) from table_cn a,table_hk b where
    a.name(+) = b.name
    order by 4 desc,2 desc,3 desc);
      

  5.   

    简单点的:
    select name,count_cn,count_hk from 
    (select nvl(a.name,b.name) name,nvl(a.count,0) count_cn,nvl(b.count,0) count_hk,nvl(a.count,0)+nvl(b.count,0) 
    from table_cn a full join table_hk b on a.name = b.name
    order by 4 desc,2 desc,3 desc);
      

  6.   

    我试了使用1楼那种方法,也用了三楼那种方法(使用full join),查询条件都一样的,为什么得出的结果不一样呢?1楼的方法得出的数据比使用full join的数据要少些。请问这是为什么?
      

  7.   

    如果name存在多条,可以试试这个:
    select name,sum(nvl(count,0)) count_cn,
    (select sum(nvl(count,0)) 
     from table_hk
     where name = table_cn.name
     group by name) count_hk
    from table_cn
    group by name
    order by name,(count_cn + count_hk) desc;
      

  8.   

    少的是a.name为空的记录吧
    语句已经有解答了,我就不写了
    排序最后添上
    order by count_cn+count_hk desc
    列别名前面要定义好,要不出错
      

  9.   

    a.name(+) = b.name,a.name = b.name(+)
    这种写法是什么意思?请高人解答一下,谢谢
      

  10.   

    第一个,右连接,第二个,左连接
    比如两表以a.name = b.name条件连接
    那么左连接会把b.name为空的记录也查出来,右连接查出a.name为空的记录
    全连接是左右的合集
      

  11.   

    描述得不对,左连接是把a.name在b中没有符合条件的记录也输出
    具体的你google查下外连接
      

  12.   

    1楼的那个好像有问题,不能够满足楼主的要求。
    4楼通过两个外连接再加上union来实现,并排序,思路清晰,但要排序耗费资源。
    6楼的好!不过以前我还真的没有用过full join。
    看这样行不行。
    select nvl(T.name,t2.name) name,
    nvl(t.count,0) count_cn,
    nvl(t2.count,0)count_hk
    from table_cn T full join table_hk T2
    on T.name=T2.name;
      

  13.   


    SQL> select nvl(a.name, b.name), nvl(a.count,0) count_cn, nvl(b.count,0) count_h
    k from table_cn a full join table_hk b on a.name=b.name order by count_cn+count_
    HK desc;
      

  14.   

    select nvl(a.name, b.name),
           a.count nvl(count_cn, 0),
           nvl(b.count, 0) count_hk
      from table_cn a
      full join table_hk b on a.name = b.name;
      

  15.   

    多谢各位,我最后使用的是full join。