我有3张桌子
1位客户 -c_id -c_name
2 Debit_Master -transaction_id -c_id -AMOUNT
3 Credit_Master -transaction_id -c_id -AMOUNT
现在我想要这样的数据:每个客户的客户名称和总金额(信用额度 - 借记金额)。
我希望查询在列表视图中获取数据,其中包含两列1-客户名称,2-总金额
答案 0 :(得分:1)
试试这个SQL语句:
SELECT
Customers.c_name as CustomerName,
SUM((CASE
WHEN Credit_Master.amount IS NULL THEN 0
ELSE Credit_Master.amount END -
CASE
WHEN Debit_Master.amount IS NULL THEN 0
ELSE Debit_Master.amount END)) as TotalAmount
FROM Customers
LEFT JOIN Debit_Master on Customers.c_id = Debit_Master.c_id
LEFT JOIN Credit_Master on Customers.c_id = Credit_Master.c_id
GROUP BY Customers.c_id
答案 1 :(得分:0)
尝试这样的事情
SELECT Customers.c_name as name , (Credit_Master.amount - Debit_Master.amount) as total FROM Customers JOIN Debit_Master on Customers.c_id=Debit_Master.c_id JOIN Credit_Master on Customers.c_id=Credit_Master.c_id