我有多个行通过查询。如何对这些行进行分组以总结某些行的值并保留其余行。
set total_fees1 = (select (f.total_Fees2) from fee_Collection f where f.student_Id =minstudent_Id);
set total_fees2 = (select (f.total_Fees3) from fee_Collection f where f.student_Id =minstudent_Id);
set paid_amount1 = (select (f.paid_amount2) from fee_Collection f where f.student_Id =minstudent_Id);
set paid_amount2 = (select (f.paid_amount3) from fee_Collection f where f.student_Id =minstudent_Id);
在某些情况下,上述查询会返回 2 行。现在,如果我获得的行数超过 1 行,我必须添加paid_amount 值,并获取total_fees 字段的第一行值。
最佳答案
那就是:
set total_fees1 = (select (f.total_Fees2) from fee_Collection f where f.student_Id =minstudent_Id LIMIT 1);
set total_fees2 = (select (f.total_Fees3) from fee_Collection f where f.student_Id =minstudent_Id LIMIT 1);
set paid_amount1 = (select SUM(f.paid_amount2) from fee_Collection f where f.student_Id =minstudent_Id);
set paid_amount2 = (select SUM(f.paid_amount3) from fee_Collection f where f.student_Id =minstudent_Id);
关于mysql - 在子查询返回多行的情况下对值求和。 (我的用例是 2 行),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46988146/