在子查询返回多行时总结值。 (我的用例是2行)
问题描述:
我有多个行通过查询。我如何分组这些行来总结一些行的值并留下余下的行。在子查询返回多行时总结值。 (我的用例是2行)
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);
你应该共享的表结构,样本数据,预期产出,你有什么迄今所做的,结果是什么,你有,什么它应该是 – Hatik