首页 > 解决方案 > 如何在laravel中执行平均时忽略空值

问题描述

在 laravel 中执行平均函数时如何忽略 NULL 的值。使用下面的代码,它将空值视为零。对不起我的英语不好希望你明白我的意思谢谢。

下面是我的代码

$average2 =round( $scores->where('student_id',$id)->avg('term2_result'));

标签: phplaravel

解决方案


尝试

$average2 = round($scores->where('student_id', $id)
                        ->where('field_name', '!=', null)
                        ->avg('term2_result'));

推荐阅读