select test1.companyid,sum(test2.money) money
from test1,test2
where test1.id1=test2.id2 and test1.companyid=10
group by test1.companyid

解决方案 »

  1.   

    --是否分组? 不分组的话select [money]=sum([money])
    from test2 a
    where exists(select 1 from test1 where id1=a.id2 and companyid=10)
      

  2.   

    --如果要将companyid也显示出来select companyid=10,[money]=sum([money])
    from test2 a
    where exists(select 1 from test1 where id1=a.id2 and companyid=10)
      

  3.   

    select test1.companyid,sum(test2.money) money
    from test1,test2
    where test1.id1=test2.id2 and test1.companyid=10
    group by test1.companyid
    就是这么简单!
      

  4.   

    select money=sum(money) from test2 where id2 in (select id1 from test1 where companyid='10')
      

  5.   

    不用分组的一种:
    select sum(money) from (select  b.money from test1 a, test2 b where a.companyid = 10 and a.id1 = b.id2) as t1用分组的两种:
    1,select a.companyid,sum(b.money) from test1 a, test2 b where a.id1 = b.id2 and a.companyid = 10 group by a.companyid2,select a.companyid, sum(b.money) from test1 a join test2 b on a.companyid = 10 and a.id1 =b.id2 group by a.companyid
    结果都是一样,都通过测试,楼主可以回去测试一下。