如何从mysql获取学生的月度出勤报告?

时间:2016-04-01 04:17:27

标签: php mysql

我需要从mysql表中显示特定学生的月度出勤报告。我需要下面的结果,

我的数据库中有考勤表:

class_id ses_id sec_id student_id att_date   remarks taken
      11      1     17         13 2016-03-28       1 2016-03-28 19:22:23
      11      1     17         14 2016-03-28       1 2016-03-28 19:22:23
      11      1     17         15 2016-03-28       1 2016-03-28 19:22:23
      11      1     17         19 2016-03-28       1 2016-03-28 19:22:23
      11      1     17         20 2016-03-28       1 2016-03-28 19:22:23
      11      1     17         13 2016-03-29       1 2016-03-29 14:25:46
      11      1     17         14 2016-03-29       1 2016-03-29 14:25:46
      11      1     17         15 2016-03-29       1 2016-03-29 14:25:46
      11      1     17         19 2016-03-29       1 2016-03-29 14:25:46
      11      1     17         20 2016-04-05       0 2016-03-31 20:07:23
      10      1     36         17 2016-04-01       0 2016-04-01 08:32:21
      10      1     37         16 2016-04-01       1 2016-04-01 08:32:41
      11      1     17         17 2017-01-01       1 2016-04-01 08:52:39

我想从我的考勤表中获取数据,其中备注'1'表示存在,如下所示: 我需要mysql查询来获取如下所示的数据

请任何人帮我写查询

我的查询是

SELECT YEAR(att_date) AS y, MONTH(att_date) AS m, COUNT(DISTINCT att_date) FROM attendance WHERE class_id=11 AND student_id=15 GROUP BY y, m

但是没有达到预期的结果,如下所示

student-ID-15

CLASS   STUDENT_ID  YEAR    MONTH   TOTAL_CLASSES   TOTAL_PRESENT

11      15      2016    April         21             20
11      15      2016    May           25             25
11      15      2016    June          30             29
11      15      2016    July          18             18
11      15      2017    January       28             28

2 个答案:

答案 0 :(得分:0)

编写如下代码

$query= "SELECT * FROM tablename WHERE STUDENT_ID  ='1234'"; //condition
$result   = mysql_query($query) or  die(mysql_error()); //query result
$count    = mysql_num_rows($result); //rows it return
if($count > 0) 
{
   //all fetching
    while($row=mysql_fetch_array($result))
    {
              $CLASS   =$row['CLASS'];
               $YEAR    =$row['YEAR'];
               $MONTH   =$row['MONTH'];
               $TOTAL_CLASSES=$row['TOTAL_CLASSES'];
               $TOTAL_PRESENT=$row['TOTAL_PRESENT'];
    }
}

答案 1 :(得分:-1)

 $getquery = "SELECT * FROM attendance WHERE STUDENT_ID  ='15'";
            $result   = mysql_query($getquery) or
                    die( mysql_error());

            $count    = mysql_numrows($result);
             if( $count>0){
            while($row=mysql_fetch_array($result)){
                       $CLASS   =$row['class_id'];
                       $YEAR    =date("Y",strtotime($row['att_date']));
                       $MONTH   =date("m",strtotime($row['att_date']));
                         $section=$row['sec_id'];



                        }
    }
    then use the these datas in your display table..