返回PHP和MySQL中每个值的groupby两列索引

时间:2018-11-04 20:38:03

标签: php mysql

我有3张桌子(考试,课程和学生)

CREATE TABLE IF NOT EXISTS `course` (
 `courseID` int(20) NOT NULL AUTO_INCREMENT,
 `courseName` varchar(50) NOT NULL,
  PRIMARY KEY (`courseID`)
) 

CREATE TABLE IF NOT EXISTS `student` (
  `studentId`int(20) NOT NULL AUTO_INCREMENT,
  `Name` varchar(60) NOT NULL,
  PRIMARY KEY (`studentId`),
)
CREATE TABLE IF NOT EXISTS `exams` (
  `examID` int(11) NOT NULL  AUTO_INCREMENT ,
  `mark` int(50) NOT NULL,
  `courseID` int(10) NOT NULL,
  `studentId` int(10) NOT NULL,

PRIMARY KEY (`examID`),
FOREIGN  KEY (`courseID`) REFERENCES `course` (`courseID`) ,
FOREIGN KEY (`studentId`) REFERENCES `student` (`studentId`) )

我想为每门课程打印每个学生的排名。  例如:

学生1,数学: 2(第二高的分数)

学生1,物理: 3

检查表:

examId | mark | courseID | studentId
------------------------------------       
   1   |  18  |    1     |    2
   2   |  20  |    1     |    3
   3   |  17  |    1     |    1
   4   |  10  |    2     |    3
   5   |  20  |    2     |    1
   6   |  8   |    2     |    2

所以,我写了这个查询来总结每个课程中每个学生的所有分数:

 $query1= "SELECT exams.studentId ,course.courseName,sum(exams.value) as total
 FROM course
   INNER JOIN exams
     ON exams.courseID = pointcategory.categoryID
    GROUP BY exams.courseID, exams.studentId 
    order by courseName,total desc ;"
   $query2=  "SELECT * FROM courses" ; //return courses names 

第一个查询的输出:

studentId |   course  total
---------------------------
  1       |    Math    | 20
  3       |    Math    | 10
  2       |    Math    | 8
  3       |    Physics | 20
  2       |    Physics | 18
  1       |    Physics |17

我已经尝试过使用此代码,但效果很好,但是如果两个学生在某门课程中的总成绩相同,则ID最小的学生将排在其他人之前。

<?php
$exec = mysqli_query($con,$query2);
$course_name= array();
while( $row = mysqli_fetch_array( $exec)){
    $course_name[] = $row; // Inside while loop
}
//////////////////////////////////////////////
$exec = mysqli_query($con,$query1);
$allExams = array();
while ($row = mysqli_fetch_array($exec)) {
    // Append all rows to an array
    $allExams[] = $row;
}
$student_order=array();

for($i = 0; $i < count($course_name); $i++){
    $count=0; // count will increase for each course 
    for($j = 0; $j < count($allExams); $j++){
        if($course_name[$i]["courseName"]==$allExams[$j]["courseName"]){
            $count++;
            if($allExams[$j]["studentId"]==1){ //test the output for first student 
                //store the rank in dict
                $student_order[$course_name[$i]["courseName"]]= $count;
                break;
            }
        }
    }}
$exec = mysqli_query($con,$query2);
$course_name= array();
while( $row = mysqli_fetch_array( $exec)){
    $course_name[] = $row; // Inside while loop
}
//////////////////////////////////////////////
$exec = mysqli_query($con,$query1);
$allExams = array();
while ($row = mysqli_fetch_array($exec)) {
    // Append all rows to an array
    $allExams[] = $row;
}
$student_order=array();

for($i = 0; $i < count($course_name); $i++){
    $count=0; // count will increase for each course 
    for($j = 0; $j < count($allExams); $j++){
        if($course_name[$i]["courseName"]==$allExams[$j]["courseName"]){
            $count++;
            if($allExams[$j]["studentId"]==1){ //test the output for first student 
                //store the rank in dict
                $student_order[$course_name[$i]["courseName"]]= $count;
                break;
            }
        }
    }}
    print_r($student_order);

0 个答案:

没有答案