我正在构建一个基于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),并使用相同的列,但只在字段ID上有所不同,这让我很困扰。我觉得我应该能够在一个子查询中做到这一点,并将正确的信息拉入select中,但我只是不知道如何做到,或者这是否是正确的方法。
您的方法没有错。就我个人而言,我总是选择联接而不是子查询,我可能会选择比"ln"one_answers"lt"更有意义的别名,尽管