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

Php 如何计算所有在场学生的百分比,php,mysql,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

有两张桌子将一起工作

第一表:

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
第二表:

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

例如:在第一个表中,第二个表中使用了
AttendanceID
21
,用于完成特定科目的出勤

我想计算特定
科目的第二个表中所有学生的百分比,总人数可以通过第一个表
AttendanceID

我在PHP中所做的是:首先,我通过以下查询从第一个表中获取总数:

SELECT COUNT(AttendanceID) FROM attendances WHERE SubjectID = ? AND BatchID = ?"
一旦我从第一个表中获得特定主题和批次的总出勤人数,我将其存储在变量
$total
中,然后我编写另一个查询以从第二个表中获得出勤人数:

SELECT COUNT(AttendanceDetailID) FROM attendancedetail WHERE CollegeID = ? AND Status = 'present' AND SubjectID = ?"
获得获得的出勤率后,我将其存储在变量
$acquired
一旦我得到这两个值,我就用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=23
BatchID=6

它可以让我得到所需的结果,但我需要更好的方法来计算它,是否可以用单个查询来实现这一点

谢谢

将查询放入子选择中:

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

将查询放入子选择中:

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
根据您的示例,您甚至不需要访问第一个表。你可以直接从第二行的数据计算出来

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

试试:

    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 = ?
尝试:



嗨,它工作得很好,但它只给了我一个学生的计算。如何从where子句中计算特定批次和科目的每个学生Remove
和CollegeID=1214
,我想我试过了,但它只计算了这个学生的ID:1212,结果是:presentCount\Uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu这个学生:1212即使我删除了
和CollegeID=1214
,你也需要给我看你调用这个查询的代码。你可以编辑你的问题我正在PHPMyAdmin MySqlHi中执行你的查询它工作得很好,但是它只给了我一个学生的计算。如何从where子句中计算特定批次和科目的每个学生Remove
和CollegeID=1214
,我想我试过了,但它只计算了这个学生的ID:1212,结果是:presentCount\Uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu这个学生:1212即使我删除了
和CollegeID=1214
,你也需要给我看你调用这个查询的代码。您可以编辑您的问题我正在PHPMyAdmin MySqlits中执行您的查询在PHPMyAdmin Mysql中给我这个错误:每个派生表都必须有自己的别名它只显示一行,而不是所有学生。请告诉我一些让每个学生计算特定主题和批次的方法,记住
AttendanceID
It在PhpMyAdmin Mysql中给我这个错误:每个派生表都必须有自己的别名它只显示一行,而不是所有学生。请告诉我一些让每个学生计算特定主题和批次的方法,记住
AttendanceID
fyi的计数,函数参数名称上的开关a-roo$这->计算持久性($subjectID、$college\u id、$name、$gender、$photo、$batchID);和公共函数calcultaionOfAttendance($subjectID、$studentID、$name、$gender、$photo、$batchID){对不起,我不明白,你能详细说明一下吗?只是通过跟踪你的函数来尝试看看它在做什么;你传入了$college\u id,但计算Fattendance将其引用为$studentID;它可能会增加一些混乱,但没有什么大的。好的,谢谢你,我肯定会更改它:)仅供参考,函数参数na上的开关a-roome.$this->calcultaionOfAttendance($subjectID、$college\u id、$name、$gender、$photo、$batchID)和公共函数calcultaionOfAttendance($subjectID、$studentID、$name、$gender、$photo、$batchID){对不起,我不明白,你能详细说明一下吗?只是通过跟踪你的函数来尝试看看它在做什么;你输入了$college\u id,但计算Fattendance将其引用为$studentID;它可能会增加一些混乱,但没有什么大的。好的,谢谢你,我肯定会更改它:)它工作了!谢谢分配。请向我推荐place,我也可以学习这些复杂的查询。只要接触它,你就会学得很好。我在工作时总是写一页长的查询,呵呵。但是如果你的代码得到了正确的结果,你只需要做一些跟踪,看看它在做什么。太好了。你很惊讶你猜对了
学生
表自我惊异。我想在FB或任何地方添加你作为朋友,因为我需要像你这样的人在我的大学项目中进行一些查询。请参见ShowStateofAttendance()向我展示了学生表。太棒了。通过此
sharoonamjid@gmail.com
每当我收到任何与查询相关的问题时,我都会将其发布在Stack Overflow中,并向您发送问题链接以帮助我解决问题。这很好吗?成功了!谢谢分配。请建议我可以学习这些复杂查询的地方。只需在工作中,我一直在写一页长的查询,呵呵。但是如果你的代码得到了正确的结果,你只需要做一些跟踪,看看它在做什么。太好了。你真了不起