[英]How to count rows in a table (phpMyAdmin)
I am currently working on an attendance system using php and mysql and I want to count the number of Present/Absent/Late of that student, which is happened on the same table. 我目前正在使用php和mysql开发一个考勤系统,我想计算该学生的当前/缺席/迟到的人数,这是在同一张桌子上发生的。 Like for example the table looks like this.
例如,表格看起来像这样。
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
I want output as below : Show how many presents/absents/late is a student in a month like 我想要的输出如下:显示一个月中有多少礼物/缺席/迟到的学生
student 1 | 20 presents | 4 absents | 2 lates
I am using fpdf library but even a php code for that is a big help. 我正在使用fpdf库,但是即使是php代码也可以提供很大帮助。
Table name : attendance_records 表名:Attenance_records
Solt'n Solt'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);
}
}
You can combine SUM and CASE to achieve what you need 您可以结合使用SUM和CASE来实现所需的功能
Try using query 尝试使用查询
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
Use in phpmyadmin .. 在phpmyadmin中使用..
SELECT * FROM table_name WHERE attendance_status = absent; SELECT * FROM table_name出席人数状态=不存在;
Phpmyadmin will count it automatically.. Phpmyadmin将自动对其进行计数。
But you should code in php and mysql or mysqli .. 但是,您应该在php和mysql或mysqli中进行编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.