2015-05-26 54 views
0

我想教練和他們的課程的列表:Moodle的教練簡介和他們的課程SQL查詢

我使用下面的查詢

SELECT u.id, u.firstname, u.lastname, u.email, c.fullname 
FROM mdl_user u, mdl_role_assignments r, mdl_context cx, mdl_course c 
WHERE u.id = r.userid 
AND r.contextid = cx.id 
AND cx.instanceid = c.id 
AND r.roleid =3 
AND cx.contextlevel =50 

我只得到單過程中,需要在這方面的幫助。下面一個

SELECT u.id, u.firstname, u.lastname, u.email, c.fullname 
from mdl_context cx 
Left join mdl_course c ON cx.instanceid = c.id 
Left join mdl_role_assignments r on r.contextid = cx.id 
Left join mdl_user u on u.id = r.userid 
WHERE r.roleid =3 
AND cx.contextlevel =50 
+0

請使用顯式連接。 –

回答

0

嘗試,如果你正在使用$ DB-> get_records_sql(),那麼第一列必須是唯一的。所以你需要結合用戶標識和課程標識。

$sql = "SELECT CONCAT(c.id, '_', u.id) AS uniqueid, 
        u.id AS userid, 
        u.firstname, 
        u.lastname, 
        u.email, 
        c.id AS courseid, 
        c.fullname, 
        r.id 
      FROM {user} u 
      JOIN {role_assignments} ra ON ra.userid = u.id 
      JOIN {role} r ON r.archetype = :archetype AND r.id = ra.roleid 
      JOIN {context} cx ON cx.id = ra.contextid AND cx.contextlevel = :context 
      JOIN {course} c ON c.id = cx.instanceid"; 
$params = array('context' => CONTEXT_COURSE, 'archetype' => 'teacher') 
$trainers = $DB->get_records_sql($sql, $params); 
0

相關問題