LMSouq
moodle-core Open

Moodle: SQL for listing all users (with custom user profile fields)

D.
d.yuk
1 month ago
3 views
Problem Description
I was looking for the proper SQL query to retrieve details of all users with the role `student`. The information would include data of some custom user profile fields. For example, I have created 2 user profile fields, with the short names `Department` and `PositionRank`. I have managed to retrieve the list with the SQL below: SELECT DISTINCT u.id AS userid, department_data.data AS department, rank_data.data AS rank FROM mdl_user u JOIN mdl_role_assignments ra ON ra.userid = u.id JOIN mdl_role r ON r.id = ra.roleid AND r.shortname = 'student' JOIN mdl_user_info_data department_data ON department_data.userid = u.id JOIN mdl_user_info_field department_field ON department_data.fieldid = department_field.id AND department_field.shortname = 'Department' JOIN mdl_user_info_data rank_data ON rank_data.userid = u.id JOIN mdl_user_info_field rank_field ON rank_data.fieldid = rank_field.id AND rank_field.shortname = 'PositionRank' But I feel like there should be a better way. Could anyone please advise? Thank you.

AI-Generated Solution

Powered by LMSouq AI · GPT-4.1-mini

✓ Solution Ready
Analyzing problem and generating solution…
Was this solution helpful?
Back to Knowledge Base