我正在构建一个基于moodle(2.4)数据库的简单应用程序。数据库是 mysql 5.1.69
我需要找到本月授予的所有证书以及有关获得证书的用户和证书本身的所有相关信息。
我的sql目前是:
SELECT u.lastname, u.firstname, ln.data, lt.data, course.fullname,
c.printhours, from_unixtime(i.timecreated) AS issued,
substring_index(c.customtext, ' ', -1) AS approval
FROM mdl_certificate_issues AS i
INNER JOIN mdl_certificate AS c ON i.certificateid = c.id
INNER JOIN mdl_user AS u on i.userid = u.id
INNER JOIN mdl_course AS course ON c.course = course.id
INNER JOIN mdl_user_info_data AS ln ON i.userid = ln.userid AND ln.fieldid = 1
INNER JOIN mdl_user_info_data AS lt ON i.userid = lt.userid AND lt.fieldid = 2
WHERE i.timecreated >= unix_timestamp('2013-07-01')
AND i.timecreated < unix_timestamp('2013-08-01')
我加入同一个表 (mdl_user_info_data) 两次(作为 lt 和 ln)并使用相同的列,但仅在 fieldid 上有所不同的事实让我感到困扰。感觉就像我应该能够在单个子查询中做到这一点并将正确的信息拉入选择中 - 但我只是无法弄清楚如何做到这一点,或者这是否是正确的方法。