从许多表

时间:2017-07-11 07:06:05

标签: sql-server

我有这样的表:

1-Subjects:
----------------------------------
SubjectID  PK
SubjectName

...

2- Courses:
----------------------------------
CourseID   PK
SubjectID  FK
CourseName
Year
Semester

...

3- Students:
----------------------------------
StudentID    PK
FirstName
LastName

...

4-StudentScores:
----------------------------------
ScoreID     PK
CourseID    FK
StudentID   FK
TotalScore

...

现在我想通过年度学期查询表格,如下所示:

StudentID  |  FirstName  | LastName  | SubjectName | TotalScore

此查询适用于指定的学期;例如:2017年第二学期

请帮助我!

1 个答案:

答案 0 :(得分:0)

试试这可能会对你有所帮助

SELECT   s.StudentID
        ,s.FirstName
        ,s.LastName
        ,s.SubjectName
        ,s.TotalScore
FROM Students
INNER JOIN StudentScores c 
    ON c.StudentID = s.StudentID
INNER JOIN Courses cr 
    ON cr.CourseID = c.CourseID
WHERE DATEPART(Yr, [cr.Year]) = 2017
    AND  DATEPART(QUARTER,[cr.Semester]) = 2