结合嵌套的While语句

时间:2016-09-07 12:37:30

标签: php mysql

我有一个嵌套的while语句,它提供了我需要的结果,但效率很低,需要永远运行?关于如何将这些结合起来的任何想法?

我首先使用名为学生成绩的较大表创建一个包含我需要的所有数据的临时表。然后我得到学生和课程名称,因为我想为每个报告分组。然后我找每个学生然后得到每个作业的分数。我还想获得每个特定学习成果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'];

    }
}
}

0 个答案:

没有答案