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

时间:2019-02-20 16:58:31

标签: php mysql fpdf

我目前正在使用php和mysql在一个出勤系统上,我想计算该学生的现在/缺席/迟到的人数,该次数在同一张桌子上发生。例如,表格看起来像这样。

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代码也可以提供很大帮助。

表名:Attenance_records

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

2 个答案:

答案 0 :(得分:3)

您可以结合使用SUMCASE来实现所需的功能

尝试使用查询

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

答案 1 :(得分:-2)

在phpmyadmin中使用。.

SELECT * FROM table_name出席人数状态=不存在;

Phpmyadmin将自动对其进行计数。

但是您应该使用php和mysql或mysqli ..