如何从两个表中求和 [英] how to take sum from two tables
问题描述
大家好,
我有两张桌子. table1和table2如下
表1
身份证日期金额
1 10/01/2011 100
1 16/01/2011 100
1 17/01/2011 100
2 01/01/2011 200
2 15/01/2011 200
2 18/01/2011 200
2 20/01/2011 200
3 10/01/2011 500
表2
ID名称地址
1哈里·悉尼
2巴里·珀斯
3 Cherry伦敦
我要上述三个人的身份证,姓名,地址,总和(金额)如下
1哈里-悉尼-300
2巴里-珀斯-800
3樱桃-伦敦-500
谢谢
Hi all,
I have two tables. table1 and table2 as follows
Table1
id date amount
1 10/01/2011 100
1 16/01/2011 100
1 17/01/2011 100
2 01/01/2011 200
2 15/01/2011 200
2 18/01/2011 200
2 20/01/2011 200
3 10/01/2011 500
Table2
id name address
1 harry sydney
2 barry perth
3 cherry london
I want id, name ,address ,sum(amount) of the above three persons as follows
1-harry - sydney - 300
2-barry - perth - 800
3-cherry - london - 500
Thanks
推荐答案
尝试如下查询.
Try as below Query.
select Table1.id, Table2.name, Table2.address, sum(Table1.amount)
from Table2 inner join Table1 on Table2.id=Table1.id
group by Table1.id, Table2.name, Table2.address;
尝试一下:
Try this:
select id, name, address, sum(amount) from table2 group by id, name, address
这篇关于如何从两个表中求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!