简体   繁体   English

SQL查询:添加总小时数

[英]SQL Query: Adding total hours

I currently have 5 tables in MySQL database. 我目前在MySQL数据库中有5个表。 Some of them share foreign keys and are interdependent of each other. 它们中的一些共享外键并且彼此相互依赖。 I am displaying classes accordingly to their majors. 我正在向他们的专业展示相应的课程。 I have two majors: Computer Engineer and Information System. 我有两个专业:计算机工程师和信息系统。 Their values are stored in a table named major. 它们的值存储在名为major的表中。 Their respective ID is 1 and 2. I am having difficulties totaling the hours of all course. 他们各自的身份证是1和2.我在所有课程的总时数上遇到了困难。 How can I add each course hours and show the total hours for the major at the bottom of the page? 如何添加每个课程时间并显示页面底部专业的总小时数?

<?


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);

?>      

Current Output 电流输出

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

You could add a variable eg $tothours then in the while clause add $row['hours'] to it. 您可以在while子句中添加一个变量,例如$tothours然后添加$row['hours']

Might be better than another query/join in the sql statement. 可能比sql语句中的另一个查询/连接更好。

Example

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

Sorry if my syntax is incorrect. 对不起,如果我的语法不正确。

This query shows the sum of your course hours grouped by major: 此查询显示按主要分组的课程时数总和:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM