SQL学生数据库

时间:2013-04-04 23:45:18

标签: sql count

我很难在学生的专栏中显示课程数量。

我有以下查询,但我不确定COUNT究竟是在计算什么,以及如何显示每个学生的计数:

CREATE VIEW Busy_Student AS
SELECT First_Name, Last_Name, Student.Student_ID
FROM Student 
INNER JOIN Enrollment ON Enrollment.Student_ID= Student.Student_ID
GROUP BY Student.Student_ID, Student.First_Name, Student.Last_Name 
HAVING COUNT(*) > 2

为所有注册超过2个班级的学生创建一个视图调用BUSY_STUDENT,用于存储连接的名称(名字和姓氏),学生ID和注册课程数。将列命名为FULL_NAME,STUDENT_ID和ENROLL_NUM。

2 个答案:

答案 0 :(得分:0)

您的SELECT语句应该是这样的:

SELECT First_Name + ' ' + Last_Name AS [FULL_NAME],
Student.Student_ID, COUNT(*) AS [ENROL_NUM]
FROM Student 
INNER JOIN Enrollment ON Enrollment.Student_ID= Student.Student_ID
GROUP BY Student.Student_ID, Student.First_Name, Student.Last_Name 
HAVING COUNT(*) > 2

答案 1 :(得分:0)

你非常接近它:

CREATE VIEW Busy_Student AS
SELECT
    CONCAT(First_Name, ' ', Last_Name) AS FULL_NAME,
    Student.Student_ID AS STUDENT_ID,
    COUNT(Enrollment.Enrollment_id) AS ENROLL_NUM
FROM Student 
INNER JOIN Enrollment USING(Student_ID)
GROUP BY Student.Student_ID
HAVING ENROLL_NUM > 2

计数是根据与学生ID匹配的Enrollment行数完成的。由于您只给出了内连接列,因此我假设每个注册都由列Enrollment_id唯一标识。您应该根据您的架构调整该列名称。

使用

First_Name + ' ' + Last_Name AS FULL_NAME 

用于SQL Server。