我正在尝试在Moodle中编写一个SQL查询,该查询向我显示学生何时报名并完成了特定课程。如果“完成日期”为空,则认为学生尚未完成课程。这是我要捕获的数据。这是我到目前为止的内容:
SELECT u.lastname, u.firstname, u.username, c.fullname,
FROM_UNIXTIME(cx.timecompleted) as "Completion Date",Null as "Enrol_Time"
FROM prefix_course_completions as cx
JOIN prefix_course as c on cx.course = c.id
Join prefix_user as u on cx.userid = u.id
Where cx.timecompleted is not null
and c.fullname like "%refresh%"
union
SELECT u.lastname, u.firstname, u.username, c.fullname,
FROM_UNIXTIME(cc.timecompleted) as "Completion Date", Null as "Enrol_Time"
From prefix_local_recompletion_cc_cc as cc
JOIN prefix_course as c on cc.course = c.id
Join prefix_user as u on cc.userid = u.id
and c.fullname like "%refresh%"
Where cc.timecompleted IS NOT NULL
union
SELECT u.lastname, u.firstname, u.username, co.fullname, Null as "Completion
Date",
FROM_UNIXTIME(ra.timemodified,'%D %M %h:%i:%s %x') AS enrol_time FROM
prefix_role_assignments ra,
prefix_user u, prefix_context c, prefix_course co
WHERE ra.contextid = c.id AND c.contextlevel = 50 AND ra.userid = u.id AND
c.instanceid = co.id AND ra.timemodified >0
and co.fullname like "%refresh%"
order by "Completion Date" DESC
上面的查询为我提供了我想要的所有数据(学生姓名,课程名称,注册/完成日期)。但是,就目前而言,“注册日期”和“完成日期”位于不同的行中。
有人对我如何在表格的同一行上获取入学日期和完成日期有任何建议/见解吗?我想这是某种JOIN查询,但我不确定。
任何帮助将不胜感激。谢谢。
答案 0 :(得分:0)
类似这样的操作将列出已注册的用户及其完成状态。
games
请注意,用户可以使用一种以上的注册方法来注册一门课程,因此一个用户可能有多于一行。此外,用户可以具有完成记录,但是已经从课程中取消注册,因此不会被包括在内。因此,也许将联接更改为:
game_id