我有一个数据库,我想创建一个视图,允许该人调用视图名称以查看视图的“年,学期,学分”。我想做的是让它给我一年,然后是学期,然后是每学期的总学分。
所以,如果我在2010年春季有2名学生,那么它应该会返回一个结果,并且他们每班3学分,共12学分,每次2学分,依此类推第四。
EX。 '2010年春季12'
CREATE VIEW tot_cred26
AS
SELECT DISTINCT year, semester, COUNT(credits)
FROM course INNER JOIN takes;
再次感谢大家,SQL的超级新手,你们都非常乐于助人! :)
答案 0 :(得分:0)
以下代码可以解决问题:
CREATE VIEW tot_cred26
AS
SELECT year, semester, COUNT(credits) total_credits
FROM course INNER JOIN takes
group by year, semester
;
答案 1 :(得分:0)
使用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