我有两张桌子:
附表
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。
我正在尝试制作一个包含每个学生的科目和成绩的图表。 图片可能会准确显示我想要的内容。
谢谢
答案 0 :(得分: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
}