[Drupal] How to get the count of all users having a particular role in a Drupal 7 website?
One of our Drupal clients had requested us to look into the innards of their website and see if the site can contain the influx of new users without breaking a sweat. We went into action swiftly and while we were gathering the details of the site, we realized that we need to get the count of all users having a particular role. We used a simple query to get the details of the users. Read on to know how to get the count of all users having a particular role in a Drupal 7 website.
Drupal 7 stores the details of the users and roles in 3 tables
1. users 2. users_roles 3. roles
The users tables contains all the relevant details of each user like, name, uid email, things that are needed to identify a user. The different roles present in a Drupal website are kept in the roles tables mapped to the corresponding role id (rid). The users_roles tables holds the mapping between the rids and the user ids (uid). Ideally you need only the user_roles tables tables but you need to know the rid of the corresponding name.
So how do you get all in one place? That's so easy. Simply use the query below.
SELECT COUNT(uid) FROM users_roles JOIN role ON users_roles.rid = role.rid and role.name ='role_name'
Take care to substitute the role_name with role name you intend to use.
We hope that this info was useful in saving you a lot of time. If you have any queries regarding this article or any other Drupal article get in touch with us you can hope for a quick response.