有a,b,c三个表
a表是基本信息
id,sbbm,name
1 , P, 李四
2 , P, 张三
3 , P, 王五
4 , P, 钱中
b表是应收钱表
id,sbbm,name,type, sq
1 , P, 李四 ,  1 , 100
1 , P, 李四 ,  2 , 100
2 , P, 张三 ,  1 , 1000
3 , P, 王五 ,  1 , 200
4 , P, 钱中 ,  1 , 300
c表是实收钱表
id,sbbm,name,type,ss
1 , P, 李四 , 1 , 100
2 , P, 张三 , 1 , 1000
3 , P, 王五 , 1 , 200
组成一条查询语句要怎么写
结果要如下:
id,sbbm,name,type,sq, ss
1 , P , 李四 , 1 , 100, 100
2 , P , 李四 , 2 , 100, 0
3 , P , 张三 , 1 , 1000, 1000
4 , P , 王五 , 1 , 200, 200
5 , P , 钱中 , 1 , 300, 0
在线等... 

解决方案 »

  1.   

    select a.id as id,a.sbbm,a.name,b.type,b.sq,c.ss
    from a,b,c
    where a.id=b.id and a.id=c.id and a.sbbm=b.sbbm and a.sbbm=c.sbbm
    and a.name=b.name and a.name=c.name and b.type=c.type
      

  2.   

    select a.id as id,a.sbbm,a.name,b.type,b.sq,c.ss 
    from a,b,c 
    where a.id=b.id(+) and a.id=c.id(+) and a.sbbm=b.sbbm(+) and a.sbbm=c.sbbm(+) 
    and a.name=b.name(+) and a.name=c.name(+) and b.type=c.type
      

  3.   


    select rownum,t.* from (select b.sbbm, b.name, b.type, b.sq, nvl(c.ss,0) ss
      from b, c
     where b.type = c.type(+)
       and b.name = c.name(+) )t
      

  4.   

    select b.id as id,b.sbbm,b.name,b.type,b.sq,nvl(c.ss,0) 
    from b left join c on b.id=c.id and b.type=c.type;
      

  5.   

    select dintinct a.id as id,a.sbbm,a.name,b.type,b.sq,c.ss 
    from a,b,c 
    where a.id=b.id and a.id=c.id and a.sbbm=b.sbbm and a.sbbm=c.sbbm 
    and a.name=b.name and a.name=c.name and b.type=c.type
      

  6.   

    select sbbm,name,type,sum(sq) sq,sum(ss) ss
    from(
    select a.sbbm,a.name,b.type,b.sq,0 ss
    from a,b
    where a.sbbm=b.sbbm
    and a.name=b.name
    union
    select a.sbbm,a.name,c.type,0 sq,c.ss
    from a,c
    where a.sbbm=c.sbbm
    and a.name=c.name)
    group by sbbm,name,type
      

  7.   

    select a.id,a.sbbm,a.name,b.TYPE,b.SQ,c.SS 
    from a,b,c
    where a.id=b.id(+) and a.sbbm=b.sbbm(+) and a.id=c.id(+) and a.sbbm=c.sbbm(+) and a.name=b.name(+) and a.name=c.name(+)
    貌似这样也有问题
      

  8.   

    select b.id,b.sbbm,b.name,b.TYPE,b.SQ,c.SS 
    from b,c
    where b.id=c.id(+) and b.sbbm=c.sbbm(+) and b.name=c.name(+) and b.type=c.type(+)
    这个可以
      

  9.   

    select b.id,b.sbbm,b.name,b.TYPE,b.SQ,nvl(c.SS,0) 
    from b,c
    where b.id=c.id(+) and b.sbbm=c.sbbm(+) and b.name=c.name(+) and b.type=c.type(+)
      

  10.   

    SQL> create table a(id int,sbbm varchar2(10),name varchar2(20));表已创建。SQL> insert into a values(1,'p','李四');已创建 1 行。SQL> insert into a values(2,'p','张三');已创建 1 行。SQL> insert into a values(3,'p','王五');已创建 1 行。SQL> insert into a values(4,'p','钱中');已创建 1 行。SQL> create table b(id int,sbbm varchar2(10),name varchar2(20),type int,sq int);表已创建。SQL> insert into b values(1,'p','李四',1,100);已创建 1 行。SQL> insert into b values(1,'p','李四',2,100);已创建 1 行。SQL> insert into b values(2,'p','张三',1,1000);已创建 1 行。SQL> insert into b values(3,'p','王五',1,200);已创建 1 行。SQL> insert into b values(4,'p','钱中',1,300);已创建 1 行。SQL> create table c(id int,sbbm varchar2(10),name varchar2(20),type int,ss int);表已创建。SQL> insert into c values(1,'p','李四',1,100);已创建 1 行。SQL> insert into c values(2,'p','张三',1,1000);已创建 1 行。SQL> insert into c values(3,'p','王五',1,200);已创建 1 行。SQL> 
    SQL> 
    SQL> 
    SQL> select sbbm,name,type,sum(sq) sq,sum(ss) ss
      2  from(
      3  select a.sbbm,a.name,b.type,b.sq,0 ss
      4  from a,b
      5  where a.sbbm=b.sbbm
      6  and a.name=b.name
      7  union
      8  select a.sbbm,a.name,c.type,0 sq,c.ss
      9  from a,c
     10  where a.sbbm=c.sbbm
     11  and a.name=c.name)
     12  group by sbbm,name,type
     13  /SBBM       NAME                       TYPE         SQ         SS
    ---------- -------------------- ---------- ---------- ----------
    p          李四                          1        100        100
    p          王五                          1        200        200
    p          张三                          1       1000       1000
    p          李四                          2        100          0
    p          钱中                          1        300          0
      

  11.   

    这个需求用不到a表,你需要的数据在b、c两个表里都有了
      

  12.   

    你的id 是一个没意义的字段,个人觉得,因为最后显示你的id有个5,请问5哪来的我觉得用个rownum就可以显示出序号了
      

  13.   

    select a.id,a.sbbm,a.name,b.sq,b.type,nvl(c.ss,0) 
    from a,b,c 
    where a.id(+)=b.id and b.type=c.type(+) 
    and a.name(+)=b.name and b.name=c.name(+)测试过了,结果就是你想要的那个
      

  14.   

    这个还真没注意,用rownum就可以
    select rownum as id,b.sbbm,b.name,b.type,b.sq,nvl(c.ss,0) 
    from b left join c on b.id=c.id and b.type=c.type;