预订的课程总数,SQL

时间:2015-05-31 17:38:01

标签: sql sum

处理查询以计算当前预订课程的总和,课程费用显示在课程表中,预订的当前课程如何显示在预订表中我将如何获得总价格的总和目前的课程预订?

课程表:

   CourseId             cost
     `````````````````````````````
     96                 300
     32                 400
     25                 500
     62                 600

预订表:

BookidId          CourseId
 `````````````````````````````
 1                 96
 2                 96
 3                 96
 4                 62

最终结果应为

CourseIdTot        Total
---------------------
96               900
62               600

需要加入但不确定如何*预订的课程总数与不同课程ID的费用。谢谢!

2 个答案:

答案 0 :(得分:2)

您可以使用加入和SUM

来完成此操作
SELECT c.courseId AS courseIdTot, SUM(cost) AS Total
FROM course AS c
INNER JOIN booking AS b
ON c.courseId = b.courseId
GROUP BY c.courseId

答案 1 :(得分:2)

尝试加入两个表格,如:

SELECT c.courseId AS courseIdTot, SUM(cost) AS Total
FROM Course c INNER JOIN Booking b
ON   c.CourseId = b.CourseId
GROUP BY c.courseId