簡體   English   中英

在mysql查詢中計算余額總和

[英]Calculate Sum of Balance in mysql query

我正在嘗試生成客戶余額未結報告。

我有兩個表: customertransaction

顧客

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
SELECT a.custname as custname 
     , sum(b.amount) as balance 
  from customer a
     , transaction b 
 where a.custid = b.customerid

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM