查看显示不正确的结果

时间:2018-07-29 09:48:22

标签: sql view

我已创建此视图以显示学生会费和总余额,其显示不正确。

就像我们为一位学生添加10条记录和3条记录一样,其显示为90加并乘以行,与付款和总余额相同...

select 
  `finance`.`students`.`studentID` AS `studentID`,
  ifnull(sum(`finance`.`dues`.`amount`),0) AS `total_dues`,
  ifnull(sum(`finance`.`payments`.`amount`),0) AS `total_payments`,
  (
    ifnull(sum(`finance`.`dues`.`amount`),0) 
    - ifnull(sum(`finance`.`payments`.`amount`),0)
  ) AS `balance` 
from (
  (`finance`.`students` left join `finance`.`payments` 
      on((`finance`.`payments`.`studentID` = `finance`.`students`.`studentID`))
  ) left join `finance`.`dues` 
      on((`finance`.`dues`.`studentID` = `finance`.`students`.`studentID`))
) 
group by `finance`.`students`.`studentID`

0 个答案:

没有答案