2017-04-15 160 views
0

我花了一整天試圖讓這個查詢工作,很想看看是否有人能指出我要去哪裏錯了。與表不扭曲數據

這是我當前的查詢:

SELECT CONCAT(STUDENT.FirstName, ' ', 
     STUDENT.LastName) AS 'Student Name', 
     `UNIT`.UnitCode, SUM(STUDENT_ASSESSMENT.MarkAwarded) AS 'Sum of Marks Awarded' 
FROM  `STUDENT_ASSESSMENT` 
JOIN  `STUDENT` 
ON  `STUDENT_ASSESSMENT`.StudentID = `STUDENT`.StudentID 
JOIN  `ASSESSMENT` 
ON  `STUDENT_ASSESSMENT`.AssessmentID = `ASSESSMENT`.AssessmentID 
JOIN  `UNIT` 
ON  `ASSESSMENT`.Unitcode = `UNIT`.UnitCode 
GROUP BY STUDENT.StudentID 
ORDER BY `STUDENT`.LastName, `STUDENT`.FirstName, `UNIT`.UnitCode 

它正常工作和做什麼,我希望它。

工作查詢結果:

enter image description here

但是我想加入一個名爲UNIT_ENROLMENT表具有的屬性我需要訪問。當我嘗試加入時,會破壞我的數據。

SELECT CONCAT(STUDENT.FirstName, ' ', 
     STUDENT.LastName) AS 'Student Name', 
     `UNIT`.UnitCode, 
     SUM(STUDENT_ASSESSMENT.MarkAwarded) AS 'Sum of Marks Awarded', 
     `UNIT_ENROLMENT`.FinalGrade 
FROM  `STUDENT_ASSESSMENT` 
JOIN  `UNIT_ENROLMENT` 
ON  `STUDENT_ASSESSMENT`.StudentID = `UNIT_ENROLMENT`.StudentID 
JOIN  `STUDENT` 
ON  `UNIT_ENROLMENT`.StudentID = `STUDENT`.StudentID 
JOIN  `ASSESSMENT` 
ON  `STUDENT_ASSESSMENT`.AssessmentID = `ASSESSMENT`.AssessmentID 
JOIN  `UNIT` 
ON  `ASSESSMENT`.Unitcode = `UNIT`.UnitCode 
WHERE `UNIT_ENROLMENT`.Year = '2017' 
GROUP BY STUDENT.StudentID 
ORDER BY `STUDENT`.LastName, `STUDENT`.FirstName, `UNIT_ENROLMENT`.Session, `UNIT`.UnitCode 

扭曲的結果:

enter image description here

如果需要任何其他信息,請發表評論。任何幫助是非常appricated

+0

請描述關係。 –

+0

學生能否擁有多個與之相關的UNIT_ENROLMENT行?除StudentID之外是否還有其他任何列必須標識唯一行?像也許單位代碼? – dev8080

+0

學生可以正確註冊多個單元,因此UNIT_ENROLMENT中的每個學生都有多個單元(不同的行)。是UnitCode也是UNIT_ENROLMENT – OldMate

回答

0

根據你分享的內容,你可以試試這個:

後加入UNIT_ENROLMENT您的加盟與評估和UNIT:

SELECT CONCAT(STUDENT.FirstName, ' ', 
    STUDENT.LastName) AS 'Student Name', 
    `UNIT`.UnitCode, 
    SUM(STUDENT_ASSESSMENT.MarkAwarded) AS 'Sum of Marks Awarded', 
    `UNIT_ENROLMENT`.FinalGrade 
FROM  `STUDENT_ASSESSMENT` 
JOIN  `STUDENT` 
ON  `STUDENT_ASSESSMENT`.StudentID = `STUDENT`.StudentID 
JOIN  `ASSESSMENT` 
ON  `STUDENT_ASSESSMENT`.AssessmentID = `ASSESSMENT`.AssessmentID 
JOIN  `UNIT` 
ON  `ASSESSMENT`.Unitcode = `UNIT`.UnitCode 
JOIN  `UNIT_ENROLMENT` 
ON  `STUDENT_ASSESSMENT`.StudentID = `UNIT_ENROLMENT`.StudentID 
AND `UNIT_ENROLMENT`.UnitCode = `ASSESSMENT`.Unitcode 
WHERE `UNIT_ENROLMENT`.Year = '2017' 
GROUP BY STUDENT.StudentID 
ORDER BY `STUDENT`.LastName, `STUDENT`.FirstName, `UNIT_ENROLMENT`.Session, `UNIT`.UnitCode 
+0

嘗試了一切,仍然沒有運氣,感謝您的幫助,雖然。 – OldMate

+0

這是什麼具體錯誤? – dev8080

+0

#1054 - 未知列'UNIT_ENROLMENT.StudentID'in'條款' – OldMate