I'm trying to get the highest score in a particular subject and then display the name of the student that has that highest score.
I have the query which gets the highest scores in each subjects in a class but it's not getting the correct student_id.
This is my query:
$subjectMaxQry = $this->db->query(
"SELECT `student_id`, max(".$totField.") AS highest,
(SELECT firstname FROM students WHERE students.id = student_id) AS firstname,
(SELECT lastname FROM students WHERE students.id = student_id) AS lastname
FROM ".$table."
WHERE `session_id` = '".$sch_setting->session_id."'
AND `class_id` = '".$class_id."'
AND `subject_id` = '".$subName['sub_id']."'");
$highestMark = $subjectMaxQry->row_array();
The query shows the highest score of each subjects in a class but the issue is that the student's name being displayed is wrong. It does not show the student's name with the highest score in a subject.