php
  • mysql
  • select
  • count
  • 2015-04-06 26 views 1 likes 
    1

    我有一個包含學生姓名和當前日期和時間,這樣MySQL的 - 通過選擇他們的名字計數爲每個用戶狀態

    約翰自己的狀態表已經3天缺席

    簡有2天缺席

    我有這樣的代碼,但它不計數爲特定用戶的所有缺席,所以我想只顯示約翰缺席一天,簡缺席一天,如果我在下拉菜單中選擇

    <?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']; 
    ?> 
    
    +0

    也許在末尾添加一個具有子句的'在'('John','jane')'中有student_name或where條件。 – 2015-04-06 10:13:10

    +0

    也許'SELECT student_name,count(*)as numberOfabsent ...' – 2015-04-06 10:27:22

    回答

    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>"; 
          } 
        ?> 
    
    相關問題