我对此查询过去2天的挑战。
我到处搜索但是徒劳无功。
我有3张桌子; customers
,transactions
和withdrawals
。
客户表有AccountNumber,Names等,交易有FK账号,金额和提款有FK账号和金额。
我只想选择AccountNumber,来自客户的姓名和SUM
相应的交易金额和SUM按账户编号提取金额账户编号。
目前我被迫开始
SELECT AccountNumber,FirstName,LastName,Town,Contact
FROM customers
WHERE status = 1
ORDER BY AccountNumber ASC
将数据加载到datatable中并循环遍历它以选择:
SELECT SUM(SusuAmount)
FROM transactions
WHERE AccountNumber = " + account + "
AND Approval = 1`
和此:
SELECT SUM(ValueAmount)
FROM withdrawals
WHERE AccountNumber = " + account + "
AND Approval = 1`
请建议一次查询以帮助完成此操作。
答案 0 :(得分:0)
尝试此查询:
SELECT cus.AccountNumber,cus.FirstName,cus.LastName,cus.Town,cus.Contract,Sum(trans.SusuAmount) AS TransactionAmount,SUM(widraw.ValueAmount) AS WithdrawalsAmount FROM customers AS cus
LEFT JOIN transactions AS tran ON (trans.AccountNumber=cus.AccountNumber AND cus.Approval=1)
LEFT JOIN withdrawals AS widraw ON (widraw.AccountNumber=cus.AccountNumber AND widraw.Approval=1)
WHERE cus.status=1 ORDER BY cus.AccountNumber ASC