select test1.companyid,sum(test2.money) money
from test1,test2
where test1.id1=test2.id2 and test1.companyid=10
group by test1.companyid
from test1,test2
where test1.id1=test2.id2 and test1.companyid=10
group by test1.companyid
from test2 a
where exists(select 1 from test1 where id1=a.id2 and companyid=10)
from test2 a
where exists(select 1 from test1 where id1=a.id2 and companyid=10)
from test1,test2
where test1.id1=test2.id2 and test1.companyid=10
group by test1.companyid
就是这么简单!
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
结果都是一样,都通过测试,楼主可以回去测试一下。