Php mysql查询中余额总和的计算
我正试图制作一份客户余额未付报告 我有两个表:Php mysql查询中余额总和的计算,php,mysql,Php,Mysql,我正试图制作一份客户余额未付报告 我有两个表:customer和transaction: 客户 custid| custname ------+------------ 1 | abc 2 | xyz 3 | ben 4 | angel 交易 tid| customerid | amount ---+-------------+------- 1 | 1 | 100 2 | 4 | 300 3 | 2
customer
和transaction
:
客户
custid| custname
------+------------
1 | abc
2 | xyz
3 | ben
4 | angel
交易
tid| customerid | amount
---+-------------+-------
1 | 1 | 100
2 | 4 | 300
3 | 2 | 130
4 | 2 | 500
5 | 3 | 100
6 | 1 | 35
7 | 4 | 104
8 | 1 | 25
9 | 3 | 50
我希望得到如下结果:
Custname | balance
----------+--------
abc | 160
xyz | 630
ben | 150
angel | 404
请注意,除了这是不正确的之外,我们大约在1992年就停止了以这种方式编写查询。来吧。加入我们。@vivek_23你的意思是什么。?你知道我代码中的好例子吗?
SELECT a.custname as custname
, sum(b.amount) as balance
from customer a
, transaction b
where a.custid = b.customerid