Php MySQL选择和平均值()

Php MySQL选择和平均值(),php,Php,我有两张桌子: 时间表 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

我有两张桌子:

时间表

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

我正试图用每个学生的科目和成绩做一个图表。 这张照片可能会显示出我想要的东西

谢谢

我解决了它:

$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
            }

这是因为学生在第二学期没有生物或化学课。您现有的逻辑比您预期的要正确。您是否有单独的主题表,如在正确规范化的数据库中?您需要itNo的内部联合查询。我只有那两张桌子,而且只用它们。。。我不能做一张新桌子。这个学生在两个学期都有生物和化学课。。。他在这些科目上没有分数。我使用了内部联接,但问题是我不知道如何正确使用它,因为它没有向我展示任何东西。你已经低效地解决了它-尝试学习在SQL语句中使用联接,你的代码将变得更简单、更快