带有SUM的MySQL内连接显示不正确的值

时间:2018-04-27 13:57:53

标签: mysql

我正在尝试生成一份包含学费列表的报告。我有以下表格。

students{**roll_number**, first_name, middle_name, last_name}
courses{**course_name**, semester, course_tuition_fees}
students_in_courses{**course_name, semester**, *roll_number*}
student_fee_payment{**receipt_number**, *roll_number*, fee_paid}

要显示在所有学期(1,2,3,4)学习的学生的费用,我使用以下查询。

select s.roll_number, s.first_name,s.middle_name,s.last_name, 

                c.course_name, sin.semester, c.course_tuition_fees, SUM(sfp.fee_paid), (c.course_tuition_fees - SUM(sfp.fee_paid)) as balance

                from students s 

                INNER JOIN students_in_courses sin on s.roll_number = sin.roll_number 

                INNER JOIN courses c on sin.course_name = c.course_name 

                INNER JOIN student_fee_payment sfp on s.roll_number = sfp.roll_number 

                where c.course_name = 'B.Ed' AND sin.semester IN (1, 2, 3, 4) AND sfp.fee_type = 'Tuition Fees' AND sfp.fee_paid > 0

                group by s.roll_number, sin.course_name, sin.semester

                order by sin.semester ASC;

查询返回记录但显示总和(fee_paid)4倍于付费。名为ALLAN WILKINSON的学生只支付了1100,但显示了4400.

我几个小时以来一直在努力解决这个问题。在这方面的任何帮助将不胜感激。

Inner join displaying wrong calculation in sum method.

0 个答案:

没有答案