SQL多个连接 - 必须是一种更简单的方法

时间:2015-01-05 22:49:34

标签: sql sql-server database select join

我有一张名为Grades的表格,我们会将学生的成绩存储到每年和他们参加考试的学期。然后我们将其显示为NCL,GCSE或Point等级。为了找到NCL / GCSE等效等级,我们在不同的表上进行查找,将点映射到正确的GCSE / NCL等效等级。

然而,这意味着我的查询有很多连接可以为我们的成绩表返回匹配的GCSE / NCL成绩,我确定必须有一个更简单的方法

我的代码如下:

SELECT        
    Grades.GradeID, Grades.PupilID, Grades.GradeType, Grades.KeyStage2, Grades.Y7T1, 
    Grades.Y7T2, Grades.Y7T3, Grades.Y8T1, Grades.Y8T2, Grades.Y8T3, Grades.Y9T1, Grades.Y9T2, 
    Grades.Y9T3, Grades.Y10T1, Grades.Y10T2, Grades.Y10T3, Grades.Y11T1, Grades.Y11T2, 
    Grades.Y11T3, GradesToPoints.GCSEGrade AS KS2, 
    GradesToPoints_Y7T2.GCSEGrade AS [Year7 Term2], 
    GradesToPoints_Y7T3.GCSEGrade AS [Year7 Term3], 
    GradesToPoints_Y7T1.GCSEGrade AS [Year7 Term1], 
    GradesToPoints_Y8T1.GCSEGrade AS [Year8 Term1], 
    GradesToPoints_Y8T2.GCSEGrade AS [Year8 Term2], 
    GradesToPoints_Y8T3.GCSEGrade AS [Year8 Term3], 
    GradesToPoints_Y9T1.GCSEGrade AS [Year9 Term1], 
    GradesToPoints_Y9T2.GCSEGrade AS [Year9 Term2], 
    GradesToPoints_Y9T3.GCSEGrade AS [Year9 Term3], 
    GradesToPoints_Y10T1.GCSEGrade AS [Year10 Term1], 
    GradesToPoints_Y10T2.GCSEGrade AS [Year10 Term2], 
    GradesToPoints_Y10T3.GCSEGrade AS [Year10 Term3], 
    GradesToPoints_Y11T1.GCSEGrade AS [Year11 Term1], 
    GradesToPoints_Y11T2.GCSEGrade AS [Year11 Term2], 
    GradesToPoints_Y11T3.GCSEGrade AS [Year11 Term3], Grades.Subject
FROM            
    Grades 
LEFT OUTER JOIN
    GradesToPoints AS GradesToPoints_Y11T3 ON Grades.Y11T3 = GradesToPoints_Y11T3.Points 
LEFT OUTER JOIN
    GradesToPoints AS GradesToPoints_Y11T2 ON Grades.Y11T2 = GradesToPoints_Y11T2.Points 
LEFT OUTER JOIN
    GradesToPoints AS GradesToPoints_Y11T1 ON Grades.Y11T1 = GradesToPoints_Y11T1.Points 
LEFT OUTER JOIN
    GradesToPoints AS GradesToPoints_Y10T3 ON Grades.Y10T3 = GradesToPoints_Y10T3.Points 
LEFT OUTER JOIN
    GradesToPoints AS GradesToPoints_Y10T2 ON Grades.Y10T2 = GradesToPoints_Y10T2.Points 
LEFT OUTER JOIN
    GradesToPoints AS GradesToPoints_Y10T1 ON Grades.Y10T1 = GradesToPoints_Y10T1.Points 
LEFT OUTER JOIN
    GradesToPoints AS GradesToPoints_Y9T3 ON Grades.Y9T3 = GradesToPoints_Y9T3.Points 
LEFT OUTER JOIN
    GradesToPoints AS GradesToPoints_Y9T2 ON Grades.Y9T2 = GradesToPoints_Y9T2.Points 
LEFT OUTER JOIN
    GradesToPoints AS GradesToPoints_Y8T3 ON Grades.Y8T3 = GradesToPoints_Y8T3.Points 
LEFT OUTER JOIN
    GradesToPoints AS GradesToPoints_Y8T2 ON Grades.Y8T2 = GradesToPoints_Y8T2.Points 
LEFT OUTER JOIN
    GradesToPoints AS GradesToPoints_Y8T1 ON Grades.Y8T1 = GradesToPoints_Y8T1.Points 
LEFT OUTER JOIN
    GradesToPoints AS GradesToPoints_Y7T3 ON Grades.Y7T3 = GradesToPoints_Y7T3.Points 
LEFT OUTER JOIN
    GradesToPoints AS GradesToPoints_Y7T2 ON Grades.Y7T2 = GradesToPoints_Y7T2.Points 
LEFT OUTER JOIN
    GradesToPoints AS GradesToPoints_Y7T1 ON Grades.Y7T1 = GradesToPoints_Y7T1.Points 
LEFT OUTER JOIN
    GradesToPoints ON Grades.KeyStage2 = GradesToPoints.Points 
LEFT OUTER JOIN
    GradesToPoints AS GradesToPoints_Y9T1 ON Grades.Y9T1 = GradesToPoints_Y9T1.Points
WHERE        
    (Grades.PupilID = @PupilID) AND (Grades.Subject = @Subject)

1 个答案:

答案 0 :(得分:2)

您的问题是您的数据模型未正确规范化。

您的成绩表似乎每位学生都有1条记录,每个年级都有一列。

相反,您的成绩表应该有每个等级的1个记录,并有一个额外的列来表示等级类型,Y7T1,Y7T2等。这也增加了灵活性:您不必更改数据库结构添加或删除某些成绩时。