我正在尝试通过查询获得学生的分数和成绩,但我无法计算出联接。这是非常复杂的数据库。有人做过吗?
下面的查询(由我自己修改的表)适用于某些学生,但不适用于其他学生,所以我需要一个具有原始表结构的正确的。
由于
MY FAULTY QUERY
SELECT mdl_grade_items.id AS ItemID,
mdl_course.shortname AS CourseShortname,
mdl_grade_items.itemname AS ItemName,
mdl_grade_items.grademax AS ItemGradeMax,
mdl_grade_items.aggregationcoef AS ItemAggregation,
mdl_grade_grades.finalgrade AS FinalGrade,
mdl_user.username AS StudentID,
mdl_user.id
FROM mdl_grade_items
INNER JOIN mdl_grade_grades
ON mdl_grade_items.id = mdl_grade_grades.itemid
INNER JOIN mdl_role_assignments
ON mdl_grade_grades.userid = mdl_role_assignments.userid
AND mdl_grade_items.courseid = mdl_role_assignments.mdlcourseid
INNER JOIN mdl_course
ON mdl_course.id = mdl_grade_items.courseid
INNER JOIN mdl_user
ON mdl_user.id = mdl_role_assignments.userid
WHERE mdl_grade_items.courseid = '2864'
AND mdl_user.username = '123456789'
答案 0 :(得分:5)
如果您忽略了学生的注册状态(如果有必要,可以稍后过滤学生),您可以简化查询。
以下是一个示例,它将为您提供课程中用户的所有成绩:
SELECT
u.id AS userid,
u.username AS studentid,
gi.id AS itemid,
c.shortname AS courseshortname,
gi.itemname AS itemname,
gi.grademax AS itemgrademax,
gi.aggregationcoef AS itemaggregation,
g.finalgrade AS finalgrade
FROM mdl_user u
JOIN mdl_grade_grades g ON g.userid = u.id
JOIN mdl_grade_items gi ON g.itemid = gi.id
JOIN mdl_course c ON c.id = gi.courseid
WHERE gi.courseid = :courseid AND u.username = :username;
如果这不适合,也许你可以从非技术角度解释你想要实现的目标。