查询所有学生总成绩:包含未选修课程的学生
To query the total score of all students, including their student ID, name, and total score, even if they haven't taken any elective courses, you can use the following SQL query:
SELECT Student.StudentID, Student.Name, SUM(Enrollment.Score) AS TotalScore
FROM Student
LEFT JOIN Enrollment ON Student.StudentID = Enrollment.StudentID
GROUP BY Student.StudentID, Student.Name;
In this query, we use a LEFT JOIN to retrieve all students from the Student table, even if they don't have any records in the Enrollment table. The SUM() function is used to calculate the total score for each student. Finally, the GROUP BY clause is used to group the results by student ID and name.
原文地址: https://www.cveoy.top/t/topic/pjyB 著作权归作者所有。请勿转载和采集!