【发布时间】:2017-12-13 04:42:40
【问题描述】:
我被困了很长时间,无法找到我的查询有什么问题,LEFT JOIN 中的debit column 有问题,下面给出的Query Result image 中显示。我的debit table 的实际值为500 但MYSQL 查询显示为1500。
我在这里做错了什么。请帮帮我。
这是我的customers table
这是我的cust_credit table
这是我的cust_debit table
MYSQL Query 给出如下
SELECT
customers.id as id,
customers.cust_name AS customer_name,
SUM(cust_debit.debit_amount) as debit,
SUM(cust_credit.credit_amount) as credit,
(SUM(cust_debit.debit_amount)) - (SUM(cust_credit.credit_amount)) as balance
FROM customers
LEFT JOIN cust_debit ON customers.id = cust_debit.cust_id
LEFT JOIN cust_credit ON customers.id = cust_credit.cust_id
GROUP BY customers.id
ORDER BY customers.id
我的Query Result 如下
【问题讨论】:
-
您正在添加 cust_debit.debit_amount 列,对于客户一,有 3 条记录,价值 500。结果 1500
-
选择 customers.id 作为 id,customers.cust_name 作为 customer_name,SUM(cust_debit.debit_amount) 作为客户的借方 LEFT JOIN cust_debit ON customers.id = cust_debit.cust_id GROUP BY customers.id ORDER BY customers.id 为什么这样可以正常工作?