MySQL左外部联接未提供正确的值

问题描述 投票:0回答:1

我有以下表格和关系

enter image description here

这里是样本数据-

enter image description here

我尝试了以下查询-

 SELECT project.*, SUM(schedule.amount) as sch_total, SUM(expense.amount) as expense_total, SUM(bill.amount) as bill_total from project 
LEFT OUTER JOIN schedule on project.project_id = schedule.project_id 
LEFT OUTER JOIN site on project.project_id = site.project_id 
LEFT OUTER JOIN expense on site.site_id = expense.site_id 
LEFT OUTER JOIN bill on site.site_id = bill.site_id 
GROUP BY project.project_id

这是查询结果-

enter image description here

但是“ bill_total”应该是200,而不是400。我在查询中错过了什么?

mysql left-join
1个回答
0
投票

您的费用表具有相同ID的多行。该表必须首先聚合,然后在连接中使用-

© www.soinside.com 2019 - 2024. All rights reserved.