多了一个m  重来四个表   tam  tad  (主从关系) 由acode关联
         ttm  ttd  (主从关系) 由tcode关联   ttm和tam由acode关联   ttd和tad由tid和did关联借钱主表    
tam      acode  
          a01借钱子表  编号为a01的借钱单借了三笔款tad       acode      did     money
           a01       0001     300
           a01       0002     600
           a01       0003     400还钱主表
ttm     tcode    acode
          t01      a01还钱子表  0001和0002对应借钱子表的0001和0002ttd     tcode    tid      tmoney
         t01     0001     250
         t01     0002     600我的问题是:列出   每一笔借钱的还钱情况  比如这样t01    0001    300    250     50
t01    0002    600    600     0
t01    0003    400            400
最后一列为剩余应该还的钱   怎么办?Waiting Online~~

解决方案 »

  1.   

    select c.tcode,c. tid ,a.money,c.tmoney,a.money - c.tmoney from 借钱子表 a,还钱主表 b,还钱子表c where a.acode = b.acode and b.tcode = c.tcode and a.did = c.tid
    这个对借还只有一条记录的情况,要是有多条,用sum() group by 搞定
      

  2.   

    我描述一下程序的原理吧有人借钱   
    借钱主表生成一条记录  子表生成N条记录   为借钱序号did和借钱数量dmoney有人还钱
    还钱主表生成一条记录(对应某条借钱主表)  
    子表生成N条记录   分别对应借钱子表的借钱序号did基本上就是这个意思
      

  3.   

    declare @tam table(acode varchar(10))
    insert into @tam select 'a01'declare @tad table(acode varchar(10),did varchar(10),[money] int)
    insert into @tad select 'a01','0001',300
    insert into @tad select 'a01','0002',600
    insert into @tad select 'a01','0003',400declare @ttm table(tcode varchar(10),acode varchar(10))
    insert into @ttm select 't01','a01'
    declare @ttd table(tcode varchar(10),tid varchar(10),tmoney int)
    insert into @ttd select 't01','0001',250
    insert into @ttd select 't01','0002',600
    select
        c.tcode,
        b.did,
        b.[money],
        d.tmoney,
        b.[money]-isnull(d.tmoney,0) as 应还
    from
        @tam a
    inner join
        @tad b
    on
        a.acode=b.acode
    inner join
        @ttm c
    on
        a.acode=c.acode
    left join
        @ttd d
    on
        c.tcode=d.tcode and b.did=d.tid
    /*
    tcode      did        money       tmoney      应还          
    ---------- ---------- ----------- ----------- ----------- 
    t01        0001       300         250         50
    t01        0002       600         600         0
    t01        0003       400         NULL        400
    */
      

  4.   

    仙林幽谷客GG   您的方法我试了  好象有点问题 比如按照上面的示例数据得出这样的结果:t01    0001    300    250     50
    t01    0002    600    600     0没有了这条
    t01    0003    400            400
      

  5.   

    哇~~  我还以为libin_ftsafe(子陌红尘)GG下班了呢你的星星把我眼睛都弄花了
      

  6.   

    --生成测试数据
    declare @tam table(acode varchar(10))
    declare @tad table(acode varchar(10),did varchar(10),money int)
    declare @ttm table(tcode varchar(10),acode varchar(10))
    declare @ttd table(tcode varchar(10),tid varchar(10),tmoney int)insert into @tam select 'a01'insert into @tad select 'a01','0001',300
    insert into @tad select 'a01','0002',600
    insert into @tad select 'a01','0003',400insert into @ttm select 't01','a01'insert into @ttd select 't01','0001',250
    insert into @ttd select 't01','0002',600--执行查询
    select
        c.tcode,
        b.did,
        b.money,
        d.tmoney,
        b.money-isnull(d.tmoney,0) as 应还
    from
        @tam a
    inner join
        @tad b
    on
        a.acode=b.acode
    inner join
        @ttm c
    on
        a.acode=c.acode
    left join
        (select tcode,tid,tmoney=sum(tmoney) from @ttd group by tcode,tid) d
    on
        c.tcode=d.tcode and b.did=d.tid--输出结果
    /*
    tcode      did        money       tmoney      应还          
    ---------- ---------- ----------- ----------- ----------- 
    t01        0001       300         250         50
    t01        0002       600         600         0
    t01        0003       400         NULL        400
    */
      

  7.   

    多了一个m  重来四个表   tam  tad  (主从关系) 由acode关联
             ttm  ttd  (主从关系) 由tcode关联   ttm和tam由acode关联   ttd和tad由tid和did关联借钱主表    
    tam      acode  
              a01借钱子表  编号为a01的借钱单借了三笔款tad       acode      did     money
               a01       0001     300
               a01       0002     600
               a01       0003     400还钱主表
    ttm     tcode    acode
              t01      a01还钱子表  0001和0002对应借钱子表的0001和0002ttd     tcode    tid      tmoney
             t01     0001     250
             t01     0002     600我的问题是:列出   每一笔借钱的还钱情况  比如这样t01    0001    300    250     50
    t01    0002    600    600     0
    t01    0003    400            400
    最后一列为剩余应该还的钱   怎么办?Waiting Online~~
    select tm.tcode,
    ad.did, ad.[money],
    td.[money],
    剩余=isnull(ad.[money],0)-isnull(td.[money],0)
    from tam am
    inner join tad ad
    on am.acode=ad.acode
    left join ttm tm
    on am.acode=tm.acode
    left join ttd td
    on tm.tcode=td.tcode
    and ad.did=td.tid
      

  8.   

    select tm.tcode,
    ad.did, ad.[money],
    td.[money],
    剩余=isnull(ad.[money],0)-isnull(td.[money],0)
    from tam am
    inner join tad ad
    on am.acode=ad.acode
    left join ttm tm
    on am.acode=tm.acode
    left join ttd td
    on tm.tcode=td.tcode
    and ad.did=td.tid
      

  9.   


    谢谢红尘GG~~~~~~~~~~~~~~~~
    谢谢红尘GG~~~~~~~~~~~~~~~~
    谢谢红尘GG~~~~~~~~~~~~~~~~
    谢谢红尘GG~~~~~~~~~~~~~~~~
    谢谢红尘GG~~~~~~~~~~~~~~~~
    谢谢红尘GG~~~~~~~~~~~~~~~~
    谢谢红尘GG~~~~~~~~~~~~~~~~
    谢谢红尘GG~~~~~~~~~~~~~~~~
      

  10.   

    晕啊  我哭死啊   我忘了说一个最关键的东西了还钱可以分次还啊红尘GG和钻石王老五GG恐怕那样写不行吧
      

  11.   

    用马甲来说明一下   刚才忽略了多次还钱的情况  唉四个表   tam  tad  (主从关系) 由acode关联
             ttm  ttd  (主从关系) 由tcode关联   ttm和tam由acode关联   ttd和tad由tid和did关联借钱主表    
    tam      acode  
              a01借钱子表  编号为a01的借钱单借了三笔款tad       acode      did     money
               a01       0001     300
               a01       0002     600
               a01       0003     400还钱主表   多了一条还钱记录  
    还可以继续有噢  
    如果钱没还完的情况下ttm     tcode    acode
              t01      a01
              t02      a01 还钱子表  0001和0002对应借钱子表的0001和0002ttd     tcode    tid      tmoney
             t01     0001     250
             t01     0002     350         t02     0001     10
             t02     0002    120  我的问题是:列出   每一笔借钱的还钱情况  比如这样t01    0001    300    260(250+10)     40
    t01    0002    600    470(350+120)    130
    t01    0003    400                    400
    最后一列为剩余应该还的钱   怎么办?Waiting Online~~
      

  12.   

    select tm.tcode,
    ad.did, ad.[money],
    t.[money],
    剩余=isnull(ad.[money],0)-isnull(t.[money],0)
    from tam am
    inner join tad ad
    on am.acode=ad.acode
    left join(
    select tm.code, td.tid, [money]=sum(td.[money])
    from ttm tm
    inner join ttd td
    on tm.tcode=td.tcode
    group by tm.code, td.tid
    )t
    on am.acode=t.acode
    and ad.did=t.tid
      

  13.   

    还钱可以分次还啊
    ------------------------------------------------------------------------------------------------------------------
    第二次给你的代码已经考虑了这个情况,所以在对还钱明细表进行了分组统计:(select tcode,tid,tmoney=sum(tmoney) from @ttd group by tcode,tid) d
      

  14.   

    第一次看到钻石。hoho~~~~
    牛。