我从Moodle SQL脚本中得到了这段SQL代码。如何使结果包括所有项目名称,无论它是否有分数prefix_grade_grade表格包含分数prefix_grade_items表具有itemname不确定是哪个供应商,这些脚本在Moodle LMS开源系统中使用
SELECT u.firstname AS 'Name' , u.lastname AS 'Surname', c.fullname AS 'Course', cc.name AS 'Category',
CASE WHEN gi.itemtype = 'Course'
THEN c.fullname + ' Course Total'
ELSE gi.itemname
END AS 'Item Name', ROUND(gg.finalgrade / gg.rawgrademax * 100 ,2) AS Percentage
FROM prefix_course AS c
JOIN prefix_context AS ctx ON c.id = ctx.instanceid
JOIN prefix_role_assignments AS ra ON ra.contextid = ctx.id
JOIN prefix_user AS u ON u.id = ra.userid
JOIN prefix_grade_grades AS gg ON gg.userid = u.id
JOIN prefix_grade_items AS gi ON gi.id = gg.itemid
JOIN prefix_course_categories AS cc ON cc.id = c.category
WHERE gi.courseid = c.id AND gi.itemname != 'Attendance' AND gi.itemmodule != 'scorm'
ORDER BY `Name` ASC
您可能只需切换grade_grades和grade_items的JOIN,然后对grade_grade执行LEFT JOIN。我还将WHERE条件移动到gi JOIN:
JOIN prefix_grade_items AS gi ON gi.courseid = c.id AND gi.itemname != 'Attendance' AND gi.itemmodule != 'scorm'
LEFT JOIN prefix_grade_grades AS gg ON gg.userid = u.id AND gg.itemid = gi.id
Moodle可以使用Mysql、Postgresql、MariaDB、SQL Server和Oracle,因此查询是通用的。这里有一些可用于跨数据库兼容查询的功能:https://docs.moodle.org/dev/Data_manipulation_API#SQL_compatibility_functions