如何计算 table 中的行数 (phpMyAdmin)

How to count rows in a table (phpMyAdmin)

我目前正在使用 php 和 mysql 开发出勤系统,我想计算那个学生的 Present/Absent/Late 人数,这发生在同一个 table。例如 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

我想要如下输出:显示一个月内有多少 presents/absents/late 学生,例如

student 1    | 20 presents       |  4 absents   | 2 lates

我正在使用 fpdf 库,但即使是 php 代码也有很大帮助。

Table 姓名:attendance_records

索尔特恩

    $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);
    }
}

在phpmyadmin中使用..

SELECT * 来自 table_name 其中 attendance_status = 不存在;

phpmyadmin会自动统计..

但您应该在 php 和 mysql 或 mysqli ..

中编码

您可以组合SUM and CASE来实现您所需要的

尝试使用查询

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