0

我目前在 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
4

2 回答 2

1

您可以添加一个变量,例如$tothours然后在while子句中添加$row['hours']它。

可能比 sql 语句中的另一个查询/连接更好。

例子

$tothours = 0;

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 />";
    $tothours += $row['hours']; <-- this line
}

print "<p>Total hours for Major: " . $tothours . ".</p>"

对不起,如果我的语法不正确。

于 2012-11-02T12:43:27.877 回答
1

此查询显示按专业分组的课程时间总和:

select major.id, sum(course.hours)
from course, major, major_course_xref
where course.id = major_course_xref.course_id
      and major.id = major_course_xref.major_id
group by major.id
于 2012-11-02T12:47:10.030 回答