Oracle数据库:
          table1                              table2        key   name  ..... ColoumnN           key1    name    quantity 
        001   aa    .....    xx              001       aa      20  
        002   bb    .....    yy              002       bb      15
        003   cc    .....    zz              007       gg      50
        004   dd    .....    zz
        005   ee    .....    tt
        006   ff    .....    abc
        007   gg    .....    ddf说明 :table1 的key 和table2的key关联,   key 和key1都是各自表的主键问题:这样构造一个查询出来这样的格式:       key1   name  quantity 
        001   aa    20           
        002   bb    15
        003   cc    0
        004   dd    0
        005   ee    0
        006   ff    0
        007   gg    50
多谢!!

解决方案 »

  1.   

    select t.key,t.name,nvl(t2.quantity,0)
      from table1 t,table2 t2
     where t.key = t2.key(+)
      

  2.   

    select t.key,t.name,nvl(t2.quantity,0) 
      from table1 t,table2 t2 
    where t.key = t2.key1(+)
      

  3.   

    select t.key,t.name,nvl(t2.quantity,0) from table1 t,table2 t2 where t.key = t2.key1(+)
      

  4.   

    这个代码完美的解决我的问题,只是不明白t2.key(+),能解释一下吗?多谢
      

  5.   

    楼主真应该是去看看SQL 基础知识,这就一个简单的左连接(left join)的问题
    楼上的写法是Oracle里特别的写法
    (+)在t2那边就是表示以t1为主去连接t2,也就是左连接
    它等于
    select t.key,t.name,nvl(t2.quantity,0) 
      from table1 t left join table2 t2 
    on t.key = t2.key1