2016-04-18 72 views

回答

2

你如何定義最流行的?登記的人數可能是定義最受歡迎的最有用的方法。

SELECT COUNT(*) AS enrolments, c.id, c.fullname 
FROM mdl_course c 
JOIN (SELECT DISTINCT e.courseid, ue.id AS userid 
     FROM mdl_user_enrolments ue 
     JOIN mdl_enrol e ON e.id = ue.enrolid) ue ON ue.courseid = c.id 
GROUP BY c.id, c.fullname 
ORDER BY 1 DESC, c.fullname 

更新:

使用這裏面的Moodle

$sql = "SELECT c.id, c.fullname, COUNT(*) AS enrolments 
     FROM mdl_course c 
     JOIN (SELECT DISTINCT e.courseid, ue.id AS userid 
       FROM {user_enrolments} ue 
       JOIN {enrol} e ON e.id = ue.enrolid) ue ON ue.courseid = c.id 
     GROUP BY c.id, c.fullname 
     ORDER BY 3 DESC, c.fullname"; 
$course = $DB->get_record_sql($sql, array(), IGNORE_MULTIPLE); 

echo $course->fullname; 
+0

在該表中,我會找到註冊的場數? –

+0

在上面的查詢中,註冊是註冊用戶的計數。 –

+0

它顯示了隨機順序的所有課程的名稱,而不是根據註冊數量。 –