我花了一整天試圖讓這個查詢工作,很想看看是否有人能指出我要去哪裏錯了。與表不扭曲數據
這是我當前的查詢:
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
它正常工作和做什麼,我希望它。
工作查詢結果:
但是我想加入一個名爲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
扭曲的結果:
如果需要任何其他信息,請發表評論。任何幫助是非常appricated
請描述關係。 –
學生能否擁有多個與之相關的UNIT_ENROLMENT行?除StudentID之外是否還有其他任何列必須標識唯一行?像也許單位代碼? – dev8080
學生可以正確註冊多個單元,因此UNIT_ENROLMENT中的每個學生都有多個單元(不同的行)。是UnitCode也是UNIT_ENROLMENT – OldMate