创建视图语句中的 Counting/totaling 行,mysql
Counting/totaling rows in a create view statement, mysql
我有一个数据库,我想创建一个视图,允许用户调用视图名称来查看视图的 "year, semester, credits"。我想要做的是让它给我年份,然后是学期,然后是每学期的总学分。
所以,如果我在 spring 2010 年有 2 个学生,并且他们每个 class 在 3 个学分时每个 class 上了 2 classes,那么结果应该是 return总共12个学分,以此类推第四。
EX。 '2010 Spring 12'
CREATE VIEW tot_cred26
AS
SELECT DISTINCT year, semester, COUNT(credits)
FROM course INNER JOIN takes;
再次感谢大家,SQL 的超级新手,你们真的很有帮助! :)
下面的代码可能会解决问题:
CREATE VIEW tot_cred26
AS
SELECT year, semester, COUNT(credits) total_credits
FROM course INNER JOIN takes
group by year, semester
;
使用GROUP BY
合并每组值的行。既然你想要总学分,你应该使用 SUM
; COUNT
只是计算行数。而您的 INNER JOIN
需要一个 ON
子句来指定两个表的关联方式;我假设两个表中都有一个 courseid
列。
CREATE VIEW tot_cred26 AS
SELECT year, semester, SUM(credits) as total_credits
FROM course AS c
INNER JOIN takes as t ON t.courseid = c.courseid
GROUP BY year, semester
我有一个数据库,我想创建一个视图,允许用户调用视图名称来查看视图的 "year, semester, credits"。我想要做的是让它给我年份,然后是学期,然后是每学期的总学分。
所以,如果我在 spring 2010 年有 2 个学生,并且他们每个 class 在 3 个学分时每个 class 上了 2 classes,那么结果应该是 return总共12个学分,以此类推第四。
EX。 '2010 Spring 12'
CREATE VIEW tot_cred26
AS
SELECT DISTINCT year, semester, COUNT(credits)
FROM course INNER JOIN takes;
再次感谢大家,SQL 的超级新手,你们真的很有帮助! :)
下面的代码可能会解决问题:
CREATE VIEW tot_cred26
AS
SELECT year, semester, COUNT(credits) total_credits
FROM course INNER JOIN takes
group by year, semester
;
使用GROUP BY
合并每组值的行。既然你想要总学分,你应该使用 SUM
; COUNT
只是计算行数。而您的 INNER JOIN
需要一个 ON
子句来指定两个表的关联方式;我假设两个表中都有一个 courseid
列。
CREATE VIEW tot_cred26 AS
SELECT year, semester, SUM(credits) as total_credits
FROM course AS c
INNER JOIN takes as t ON t.courseid = c.courseid
GROUP BY year, semester