php - 求每个学生占所有出勤率的问题

标签 php mysql laravel

我的目标是根据在场或缺席的情况,让学生也具有其他属性。我已经搜索了所有内容,但我不知道如何在 Laravel 中实现它,我很困惑,我不知道为什么。

我首先厌倦了获取总数,但没有任何效果。请帮助我在 laravel 查询生成器中完成此操作。谢谢

DB::table('attendances')->
    select(
        'courses.name as course_name',
        'sections.name as section_name',
        'students.name as student_name',
        'students.rollnumber as student_rollnumber',
        'attendances.is_present as is_present',
        'attendances.date as date',
        DB::raw('COUNT(attendances.id) as tcount')
    )
    ->join('courses','courses.id','attendances.course_id')
    ->join('sections','sections.id','attendances.section_id')
    ->join('students','students.id','attendances.student_id')
    ->where('attendances.student_id',$id)
    ->where('attendances.course_id',$course_id)
    ->where('attendances.section_id',$section_id)
    ->groupBy('attendances.id')
    ->get();

我的预期结果是,我将获得属性名称为百分位的所有其他信息。提示(我在出勤表调用 is_presnet 中有一个属性,其值为 0 或 1)

最佳答案

据我了解,您需要获得给定类(class)的每个学生的出勤率百分比。

在 SQL 查询中,不需要按 attends.id 分组,因为每个出勤 ID 都是唯一的。如果删除以下两行:

DB::raw('COUNT(attendances.id) as tcount')

->groupBy('attendances.id')

它应该返回学生在给定类(class)中的所有出勤情况。您可以使用 Php 循环结果数据。在循环中,您可以检查当前有多少行。如果将礼物总数除以出勤总数,您将得到给定类(class)的学生出勤百分比

关于php - 求每个学生占所有出勤率的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57236689/

相关文章:

Mysql查询求和十进制数

php - 如何重置 laravel 用户删除中的自动增量?

php - 从 formatCurrency 中删除货币符号

php - 无法将使用 php openssl 生成的 RSA key 导入到 windows CryptoAPI

php - MYSQL - 输出不正确?

Laravel 4.2 SuperClosure : Attempted to serialize unserializable builtin class Closure

php - Laravel 5 Lang::get() 替换

php - 查找 SQL 子字符串

php - laravel 5,更新用户密码

mysql - MYSQL 上 GADGETS 表的 Worklight wlapp 部署 SQL 错误