I was looking to
CONTRIB-317 ... and, after looking the first error reported there:
Unknown table 'ul' in field list
SELECT u.*, ul.timeaccess as lastaccess FROM tmdl_user u JOIN (SELECT DISTINCT ssra.userid FROM tmdl_role_assignments ssra WHERE ssra.contextid IN (1,4,5) AND ssra.roleid IN (3,8) ) ra ON ra.userid = u.id WHERE u.deleted = 0 ORDER BY ul.timeaccess DESC
line 686 of lib/dmllib.php: call to debugging()
line 944 of lib/dmllib.php: call to get_recordset_sql()
line 4576 of lib/accesslib.php: call to get_records_sql()
line 749 of lib/deprecatedlib.php: call to get_users_by_capability()
line 324 of mod/certificate/lib.php: call to get_course_teachers()
line 570 of mod/certificate/lib.php: call to certificate_email_teachers()
line 112 of mod/certificate/view.php: call to certificate_issue()
I've taken a look to get_users_by_capability()
And it seems that the ul.timeaccess is added both to the select list and the order by clause, while the ul alias hasn't been defined at all in the query.
I really don't know what's the solution for this (adding a table in the join above...) but seems clearly that sometimes it can generate wrong SQL.