SQL查询:添加总小时数

时间:2012-11-02 12:36:23

标签: php

我目前在MySQL数据库中有5个表。它们中的一些共享外键并且彼此相互依赖。我正在向他们的专业展示相应的课程。我有两个专业:计算机工程师和信息系统。它们的值存储在名为major的表中。他们各自的身份证是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

2 个答案:

答案 0 :(得分: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>"

很抱歉,如果我的语法不正确。

答案 1 :(得分: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