Moodle SQL-临时数据库查询(合并两个查询)

时间:2019-02-06 14:08:42

标签: mysql sql moodle

我一直在使用两个单独的SQL查询从Moodle(v3.5)中提取基本用户信息,这是我进行离线计费所需要的。

下面的第一个查询提供了大多数基本细节,但是,它没有提供用户报名参加特定课程的日期。

用户可能已在1月1日在门户网站上注册,他可能在1月15日注册了课程1,然后在2月15日注册了课程2。我需要跟踪特定课程的实际注册日期。

下面的第二个SQL查询在最后一列中给出每个课程的实际注册日期。但是它不提供电子邮件ID,也不提供自定义字段-省份和城市。

当前,我运行两个查询,将它们带到电子表格,并使用每个报告第一列中的UserID进行Vlookup。这是非常不准确的,因为相同的用户ID会重复多次,而excel会进行首次查找。

简而言之:

我只需要将第二个查询的最后一列中的“课程注册日期”添加到下面的第一个查询中即可。

我已尽我所能,尝试了SQL技能(至多可悲)。

衷心感谢您在以下查询中提供的帮助。

查询1

SELECT

user2.id AS userid,

user2.username AS username,

user2.firstname AS Firstname,

user2.lastname AS Lastname,

user2.email AS Email,


(SELECT UID.data FROM prefix_user_info_field UIF, prefix_user_info_data UID WHERE UID.fieldid = UIF.id AND UIF.shortname = 'usercity' AND UID.userid = user2.id) as 'usercity',

(SELECT UID.data FROM prefix_user_info_field UIF, prefix_user_info_data UID WHERE UID.fieldid = UIF.id AND UIF.shortname = 'userstate' AND UID.userid = user2.id) as 'state',

(SELECT UID.data FROM prefix_user_info_field UIF, prefix_user_info_data UID WHERE UID.fieldid = UIF.id AND UIF.shortname = 'mobilephone' AND UID.userid = user2.id) as 'mobilephone',

course.fullname AS Course

FROM prefix_course AS course 

JOIN prefix_enrol AS en ON en.courseid = course.id

JOIN prefix_user_enrolments AS ue ON ue.enrolid = en.id

JOIN prefix_user AS user2 ON ue.userid = user2.id

查询2

SELECT 
u.id,
u.username,
u.lastname,
u.firstname,
c.fullname,

DATE_FORMAT(FROM_UNIXTIME(ue.timecreated), '%Y-%m-%d %H:%i') AS 'Timecreated' FROM prefix_user_enrolments ue LEFT JOIN prefix_enrol e ON (ue.enrolid = e.id) LEFT JOIN prefix_course c ON (e.courseid = c.id) LEFT JOIN prefix_user u ON (ue.userid = u.id)

0 个答案:

没有答案