I have a nested while statement that gives the results I need but is very inefficient and takes forever to run? Any thoughts on how these could be combined?
I first create a temporary table with all the data I need from a larger table called student grades. I then get the students and course names because I want to group these for each report. I then look for each student then get the scores for each assignment. I also want to get the average of the only of the last three assignments for each particular learning outcome ID.
//MYSQL query to create temp table
$qryTmp = "CREATE TEMPORARY TABLE IF NOT EXISTS tmp AS (SELECT studentsisid,coursename,assessmenttitle,studentname,outcomescore,recordid,learningoutcomeid,learningoutcomename,assessmentid
from studentgrades
WHERE studentsisid LIKE '$studentid' AND coursename LIKE '$coursename')";
mysql_query($qryTmp);
//MYSQL query Coursename and student name
$qryCourse = "SELECT studentsisid,coursename,studentname
from tmp
WHERE studentsisid LIKE '$studentid' AND coursename LIKE '$coursename' GROUP BY studentsisid,coursename ORDER by studentname,coursename";
$resultCourse=mysql_query($qryCourse);
while ($rowCourse = mysql_fetch_assoc($resultCourse)) {
$studentid = $rowCourse['studentsisid'];
$name = $rowCourse['studentname'];
$coursename = $rowCourse['coursename'];
//MYSQL query Learning Outcome ID
$qryLID ="SELECT outcomescore,learningoutcomeid,count(RecordID) as assessmentcount
from tmp
WHERE studentsisid = '$studentid' AND coursename='$coursename'
Group by studentsisid,coursename, learningoutcomeid ORDER BY studentname, coursename";
$resultLID=mysql_query($qryLID);
while ($rowLID = mysql_fetch_assoc($resultLID)) {
$learningoutcomeid = (int)$rowLID['learningoutcomeid'];
$assessmentcount = (int)$rowLID['assessmentcount'];
//MYSQL query recent 3
$qryRecent3 = "SELECT avg(outcomescore) as recentscoreavg
FROM
(SELECT outcomescore FROM tmp WHERE
studentsisid='$studentid' AND coursename='$coursename' AND learningoutcomeid='$learningoutcomeid'
ORDER BY AssessmentID DESC LIMIT 3) as r";
$resultRecent3=mysql_query($qryRecent3);
while ($rowRecent3 = mysql_fetch_assoc($resultRecent3)) {
$recentscoreavg = number_format($rowRecent3['recentscoreavg'], 1);
$assessmentcount = $rowRecent3['recentover3'];
}
}
}