mysql - 在子查询返回多行的情况下对值求和。 (我的用例是 2 行)

标签 mysql sql stored-procedures

我有多个行通过查询。如何对这些行进行分组以总结某些行的值并保留其余行。

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/

相关文章:

mysql - 不同的代码 : Delimiter//and Delimiter $$

Mysql 一对一或多对连接

MySQL INSERT SELECT 语句抛出错误 1062 : Duplicate entry '' for key 2

mysql - 列数、整数值、数据被截断和其他一些消息

mysql - 在 MYSQL 中拆分和匹配字符串

stored-procedures - mariadb程序phpmyadmin报错

stored-procedures - 使用 PetaPoco 执行存储过程

PHP:返回 Laravel 4 中更新行的 id

sql - 无效的列名 '-'

sql - 为 Sql Developer 导入设置默认列值