我目前正在使用 php 和 mysql 开发一个考勤系统,我想计算该学生在同一张 table 上发生的出席/缺席/迟到的次数。例如,表格看起来像这样。
student_name | attendance status | date
| |
student1 | Present | 2019-02-21
student2 | Absent | 2019-02-21
student3 | Late | 2019-02-21
student1 | Absent | 2019-02-22
student2 | Absent | 2019-02-22
student3 | Present | 2019-02-22
我想要如下输出:显示学生一个月内有多少次出席/缺席/迟到
student 1 | 20 presents | 4 absents | 2 lates
我正在使用fpdf 库,但即使是 php 代码也是一个很大的帮助。
表名称:出勤记录
Solr 'n
$result = mysqli_query($conn, "
SELECT student_name,
SUM(CASE WHEN attendance = 'Present' THEN 1 ELSE 0 END) AS presents,
SUM(CASE WHEN attendance = 'Absent' THEN 1 ELSE 0 END) AS absents,
SUM(CASE WHEN attendance = 'Late' THEN 1 ELSE 0 END) AS lates
FROM attendance_records
GROUP BY student_name
") or die("database error:". mysqli_error($conn));
foreach( $result as $row ) {
$pdf->SetFont('Arial','I',9);
$pdf->Ln();
foreach($row as $column) {
$pdf->Cell(39,10,$column,1);
}
}
最佳答案
尝试使用查询
SELECT student_name,
SUM(CASE WHEN attendance = 'Present' THEN 1 ELSE 0 END) AS presents,
SUM(CASE WHEN attendance = 'Absent' THEN 1 ELSE 0 END) AS absents,
SUM(CASE WHEN attendance = 'Late' THEN 1 ELSE 0 END) AS lates
FROM attendance_records
GROUP BY student_name
关于php - 如何计算表中的行数 (phpMyAdmin),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54791664/