1

我正在尝试通过查询获取学生的分数和成绩,但我无法计算出联接。这是一个非常复杂的数据库。以前有人做过吗?

下面的查询(我自己修改的表格)适用于某些学生,但不适用于其他学生,所以我需要一个具有原始表结构的合适的查询。

谢谢

我的错误查询

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'
4

1 回答 1

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;

如果这不适合,也许您可​​以从非技术角度解释您要达到的目标。

于 2012-11-28T05:59:22.953 回答