mysql - 通过选择名称来计算每个用户的状态

时间:2015-04-06 10:10:18

标签: php mysql select count

我有一张表,其中包含学生姓名,当前日期和时间以及他们的状态

  

约翰缺席3天

     

Jane缺席了2天

我有这个代码,但它计算所有缺席不是特定用户所以我想只显示约翰缺席日和jane缺席日,如果我在下拉菜单中选择它

<?php
$sql = "SELECT student_name, count(status) as numberOfabsent FROM attendance WHERE status = 'Absent' GROUP BY student_name";
        $result = $db->prepare($sql);
        $result->execute();
        while ($row = $result->fetch()) 
        echo $row['numberOfabsent'];
 ?>

1 个答案:

答案 0 :(得分:0)

    <?php
             $sql = "SELECT *, count(status) as numberOfabsent FROM attendance WHERE status = 'Absent' GROUP BY student_name";
             $result = $db->prepare($sql);
             $result->execute();
             while ($row = $result->fetch()) {
             $numberOfabsent = $row['numberOfabsent'];
             $absdate = $row['timestamp'];
             echo "<tr><td><a href='" . $configs->getConfig('WEB_ROOT') . "admin/adminuseredit.php?usertoedit=" . $row['student_name'] . "'>" . $row['student_name'] . "</a></td>"
             . "<td><div class='shorten'><a href='" . $row['numberOfabsent'] . "'>" . $numberOfabsent  . " days</a></div></td><td>" . $absdate . "</td>"
             . "<td class='text-center'><div class='btn-group btn-group-xs'><a href='".$configs->getConfig('WEB_ROOT')."admin/adminuseredit.php?usertoedit=".$row['student_name']."' title='Edit' class='open_modal btn btn-default'><i class='fa fa-pencil'></i> View</a>";
             echo"</tr>";
             }
     ?>