如何计算所有在场学生的百分比

时间:2016-08-06 08:59:41

标签: php mysql

有两个表可以一起使用。

第一张表:

AttendanceID TeacherID BatchID SubjectID SemesterID Date 
          32       110       8         9          1 2016-08-04 
          31       102       8        10          1 2016-07-17 
          30       108       6        22          3 2016-06-27 
          29       109       7        18          2 2016-06-27 
          28       109       8        13          1 2016-06-27 
          27       110       7         7          2 2016-06-27 
          26       110       8         9          1 2016-06-27 
          25       104       2        42          7 2016-04-20 
          24       104       5        35          5 2016-04-14 
          23       104       2        42          7 2016-04-14
          22       102       2        41          7 2016-04-13 
          21       102       2        41          7 2016-04-10 
          20       102       6        23          3 2016-04-10 
          19       102       6        23          3 2016-04-10

第二张表:

enter image description here

第一个表由唯一的行组成,这些行将用于表2中的每个新的出勤列表。

例如:在第一个表格中,AttendanceID 21用于第二个表格,用于完成特定主题的出席。

我想计算特定Subject第二个表格中所有学生的百分比,并且可以通过第一个表AttendanceID

获得总数

我在PHP中所做的是:首先,我使用此查询从第1个表中获取总数:

SELECT COUNT(AttendanceID) FROM attendances WHERE SubjectID = ? AND BatchID = ?"

一旦我从第一个表中获得特定主题和批次的出席总数,我将其存储在变量$total中,然后我写了另一个查询,以便从第二个表中获得出席率:

SELECT COUNT(AttendanceDetailID) FROM attendancedetail WHERE CollegeID = ? AND Status = 'present' AND SubjectID = ?"

获得出席后我将其存储在变量$obtained中 一旦我得到两个值,然后我用这样计算PHP中的百分比:

    if(!empty($total) && !empty($obtained)) {
    $result = (($obtained * 100)/ $total);
        $result = round($result);
    }

以下是PHP的完整代码:

    public function showStateOfAttendance($subjectID, $batchID){

 $st = $this->conn->prepare("SELECT CollegeID, Name, Gender, Photo FROM students WHERE BatchID = ?");
    $st->bind_param("i", $batchID);
    $st->execute();
    $st->store_result();
    $num_rows = $st->num_rows;
    $st->bind_result($college_id, $name, $gender, $photo);


    $this->response['attendance'] = array();
    while($st->fetch()) {

        $this->calcultaionOfAttendance($subjectID, $college_id, $name,  $gender, $photo, $batchID);



    }
     return json_encode($this->response);
    $st->free_result();
    $st->close();

}


public function calcultaionOfAttendance($subjectID, $studentID, $name, $gender, $photo,  $batchID) {

      $stmt = $this->conn->prepare("SELECT COUNT(AttendanceID) FROM attendances WHERE SubjectID = ? AND BatchID = ?");
    $stmt->bind_param("ii", $subjectID, $batchID);
    $stmt->execute();
    $stmt->store_result();
    $num_rows = $stmt->num_rows;
    $stmt->bind_result($AttendanceID);
    while($stmt->fetch()) {
       $total = $AttendanceID;  
    }
    $stmt->free_result();
    $stmt->close();

    $stmt2 = $this->conn->prepare("SELECT COUNT(AttendanceDetailID) FROM attendancedetail WHERE CollegeID = ? AND Status = 'present' AND SubjectID = ?");
    $stmt2->bind_param("ii", $studentID, $subjectID);
    $stmt2->execute();
    $stmt2->store_result();
    $stmt2->bind_result($AttendanceDetailID);
    while($stmt2->fetch()){
        $obtained = $AttendanceDetailID;
    }
    if(!empty($total) && !empty($obtained)) {
    $result = (($obtained * 100)/ $total);
        $result = round($result);
       $rating = ($result)/20;

         $tmp = array();

          $tmp['result'] = $result;
          $tmp['total'] = $total;
          $tmp['obtained'] = $obtained;
          $tmp['rating'] = $rating;
          $tmp['name'] = $name;
          $tmp['college_id'] = $studentID;
          $tmp['gender'] = $gender;
          $tmp['photo'] = $photo;

        array_push($this->response['attendance'],$tmp);



    //var_dump(array($total, $obtained, $result, $rating, $studentID, $name));
    }else if(empty($total)) {

         $tmp = array();

          $tmp['result'] = 0.0;
          $tmp['total'] = 0.0;
          $tmp['obtained'] = $obtained;
          $tmp['rating'] = 0.0;
          $tmp['name'] = $name;
          $tmp['college_id'] = $studentID;
        array_push($this->response['attendance'],$tmp);


    //var_dump(array("0.0",$obtained, "0.0","0.0",$studentID,$name));
    }else if(empty($obtained)) {

         $tmp = array();

          $tmp['result'] = 0.0;
          $tmp['total'] = $total;
          $tmp['obtained'] = 0.0;
          $tmp['rating'] = 0.0;
          $tmp['name'] = $name;
          $tmp['college_id'] = $studentID;
        array_push($this->response['attendance'],$tmp);


    //var_dump(array($total, "0.0", "0.0","0.0", $studentID , $name));
    }


}

以下是我所做查询的Android屏幕截图:以下结果适用于SubjectID = 23BatchID = 6

enter image description here

它为我提供了所需的结果,但我需要更好的方法来计算它,是否可以使用单个查询来执行此操作?

由于

3 个答案:

答案 0 :(得分:1)

将查询放在子选择中:

SELECT
(SELECT COUNT(AttendanceDetailID) FROM attendancedetail WHERE CollegeID = ? AND Status = 'present' AND SubjectID = ?) 
/ (SELECT COUNT(AttendanceID) FROM attendances WHERE SubjectID = ? AND BatchID = ?)
* 100

或(更易读)加入两个子查询:

SELECT (obtained * 100) / total
FROM (
    SELECT COUNT(AttendanceDetailID) AS total
    FROM attendancedetail
    WHERE CollegeID = ? AND Status = 'present' AND SubjectID = ?
) t
CROSS JOIN (
    SELECT COUNT(AttendanceID) AS obtained 
    FROM attendances
    WHERE SubjectID = ? AND BatchID = ?
) a

答案 1 :(得分:1)

SELECT
   SUM(`Status` = 'present') AS presentCount,
   COUNT(*) AS totalCount,
   (SUM(`Status` = 'present') * 100) / COUNT(*) AS percent  
FROM
   details    
WHERE
   BatchID = 2 
   AND SubjectID = 41 
   AND CollegeID = 1214    
GROUP BY
   AttendanceID

根据您的示例,您甚至无需访问第一个表格。您可以直接从第二个数据中计算出来。

答案 2 :(得分:1)

尝试:

    SELECT  s.CollectID, s.Name, s.Gender, s.Photo, 
        (SELECT count(AttendanceID) from attendances WHERE SubjectID =? and BatchID = s.BatchID) as total,
        (SELECT count(AttendanceDetailID) FROM attendancedetail WHERE CollegeID = s.CollectID and Status = 'present' and SubjectID = ?) as obtained
 FROM students s
 WHERE s.BatchID = ?