У меня есть 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);
Задача ещё не решена.
Других решений пока нет …