gpt4 book ai didi

MYSQL 左连接不正确的结果

转载 作者:行者123 更新时间:2023-11-30 21:49:52 25 4
gpt4 key购买 nike

<分区>

我被困了很长时间,无法找到我的查询有什么问题,我的 LEFT JOIN 中的 debit column 有问题,显示在Query Result image 如下所示。我的 debit table 的实际值是 500MYSQL 查询显示为 1500。我在这里做错了什么。请帮助我。

这是我的客户表

customers` table

这是我的cust_credit 表

cust_credit table

这是我的cust_debit 表

cust_debit table

MYSQL查询如下

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

我的查询结果如下所示

Result

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com