表A            表B             表C
code  name     code   指标1    code   指标2
1     北京     1       200     2       300
2     上海     4       100     3       400
3     南京                     4       200
4     广州
5     杭州我想得到的查询结果:code    name    指标1    指标2
1       北京    200      NULL 
2       上海    NULL     300
3       南京    NULL     400
4       广州    100      200 
5       杭州    NULL     NULL
sql 语句应该如何写?

解决方案 »

  1.   

    一个左连接就ok
    select * from a,b,c
    where a.code=b.code(+)
    and a.code=c.code(+)
      

  2.   


    这样只是 表B 和表C 里的code和,code 5 就没有了啊
      

  3.   

    标准格式:
    SELECT * from a aa, 
    (select b.code from b )BB,
    (select c.code from c )cc
    where aa.code=bb.code(+) and aa.code=cc.code(+) ;或者 有时需要进行更为简洁的:select * from a aa group by aa.code,
    (select b.code from b group by b.code )BB,
    (select c.code from c group by c.code)cc
    where aa.code=bb.code(+) and aa.code=cc.code(+) ;
    做报表的时候很有用!