2

我有 2 张桌子:

日程

id  id_teacher       subject    class     hour    day
1   2 [->]             Math         X C     8   Monday
2   2 [->]             Math         X C     12  Wednesday
3   2 [->]             Math         X C     9   Tuesday
4   2 [->]             Math         VI B    10  Monday
5   2 [->]             Math         X C     11  Monday
6   2 [->]             Math         X C     10  Tuesday
7   5 [->]             Chemistry    X C     9   Monday
8   5 [->]             Chemistry    X C     12  Monday
9   2 [->]             Sports       X C     7   Monday
10  5 [->]             Biology      X C     11  Friday
11  2 [->]             English      X C     12  Friday
12  2 [->]             Chemistry    X C     9   Thursday

成绩

Id  id_elev     subject     date        grade   semester
4   1 [->]      English     2013-10-01  8       1
5   1 [->]      Math        2013-10-03  7       1
6   1 [->]      Math        2012-10-03  8       2
7   1 [->]      English     2013-02-28  9       2
8   4 [->]      Math        2013-10-06  5       1
9   4 [->]      English     2013-07-02  7       2
10  4 [->]      Sport       2013-10-01  9       1
11  1 [->]      Math        2013-10-03  4       1
12  1 [->]      English     2013-10-16  9       1

我想从附表中获取所有科目:

生物、化学、英语、数学、体育

$sth1 = $dbh->prepare("SELECT * FROM schedule WHERE class = :class GROUP BY subject;");
$sth1->bindParam(":class", $a);   
$sth1->execute();
while($result1 = $sth1->fetch(PDO::FETCH_ASSOC)){
     echo $result1['subject']." ";
}

$a 是一个等于 'X C' 的变量。

到现在一切正常。

现在我想选择所有科目和每个科目的平均值。

$sth = $dbh->prepare("SELECT id_student, subject, AVG(grade) FROM grades WHERE id_student = :id_student AND semester = 2 GROUP BY subject;");
$sth->bindParam(":id_student", $_SESSION['id']);   
$sth->execute();

while($result = $sth->fetch(PDO::FETCH_ASSOC)){
            echo $result['subject']." ".$result['AVG(grade)'];
}

它向我展示了:

英语 9.0000 数学 8.0000

但是我想从时间表中选择所有科目并进行老化,即使学生在该科目没有任何成绩......如果它没有回显0。

我正在尝试制作一个包含每个学生的科目和成绩的图表。图片可能会准确显示我想要的。

在此处输入图像描述

谢谢

4

1 回答 1

0

我解决它:

$a = 'X C';
$sth1 = $dbh->prepare("SELECT * FROM schedule WHERE class = :class GROUP BY subject;");
        $sth1->bindParam(":class", $a);   
        $sth1->execute();
        while($result1 = $sth1->fetch(PDO::FETCH_ASSOC)){
            $sth = $dbh->prepare("SELECT id_student, subject, AVG(grade) FROM grades WHERE id_student = :id_student AND subject = :subject AND semester = 2;");
            $sth->bindParam(":id_student", $_SESSION['id']);   
            $sth->bindParam(":subject", $result1['subject']);   
            $sth->execute();
            $result = $sth->fetch(PDO::FETCH_ASSOC);
            echo $result1['subject']." ";
            if (is_null($result['AVG(grade)'])) {
            // do stuff
            }else{
            // do stuff
            }
于 2013-11-08T10:03:01.570 回答