我目前在 MySQL 数据库中有 5 个表。其中一些共享外键并且相互依赖。我根据他们的专业显示课程。我有两个专业:计算机工程师和信息系统。它们的值存储在名为major 的表中。他们各自的 ID 是 1 和 2。我在计算所有课程的时间时遇到了困难。如何添加每个课程的学时并在页面底部显示专业的总学时?
<?
try {
$pdo = new PDO ("mysql:host=$hostname;dbname=$dbname","$username","$pw");
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}
$query = $pdo->prepare("SELECT course.name, course.code, course.description, course.hours,
CASE semester.semester
WHEN 0 THEN 'Spring'
WHEN 1 THEN 'Fall'
WHEN 2 THEN 'All-year'
END semester, semester.year
FROM course
LEFT JOIN major_course_xref ON course.id = major_course_xref.course_id
LEFT JOIN major ON major.id = major_course_xref.major_id
LEFT JOIN course_semester_xref ON course.id = course_semester_xref.course_id
LEFT JOIN semester ON course_semester_xref.semester_id = semester.id
Where major.id = '1'
");
$query->execute();
if ($query->execute()){
while ($row = $query->fetch(PDO::FETCH_ASSOC)){
print "<b>" . $row['name'] . "</b><br>";
print $row['code'] . "<br>";
print $row ['description'] . "<br>";
print $row['hours'] . " hrs. <br>";
print $row['semester'] . "<br>";
print $row['year'] . "<br>";
print "------------------------------<br />";
}
}
else
echo 'Could not fetch results.';
unset($pdo);
unset($query);
?>
电流输出
Computer Programming I
CPSC1400
Introduction to disciplined, object-oriented program development.
4 hrs.
Spring
2013
------------------------------
Computer Programming II
CPSC1500
This course builds upon the topics covered in Computer Science I and provides experience developing complex applications.
4 hrs.
Fall
2013
------------------------------
Database Programming
CPSC2100
Study of relational database management systems and information storage and retrieval techniques.
4 hrs.
Spring
2014