PDA

View Full Version : Best way to retrieve the number of users with a specific secondary usergroup?


Attilitus
08-06-2007, 07:18 PM
Is there any easy way to retrieve the amount of users who have a specific secondary usergroup? I need to be able to quickly retrieve a count, and want to know the most load-friendly way of doing it. (Without looping through the resultset to check if the groupid is in the array of membergroupids.)

Edit: for other people's reference here is how vbulletin does it:

(I guess I'm all set.)

// count secondary users
$groupcounts = $db->query_read("
SELECT membergroupids, usergroupid
FROM " . TABLE_PREFIX . "user
WHERE membergroupids <> ''
");
while ($groupcount = $db->fetch_array($groupcounts))
{
$ids = fetch_membergroupids_array($groupcount, false);
foreach ($ids AS $index => $value)
{
if ($groupcount['usergroupid'] != $value AND !empty($vbulletin->usergroupcache["$value"]))
{
$vbulletin->usergroupcache["$value"]['secondarycount']++;
}
}
}

Marco van Herwaarden
08-07-2007, 07:19 AM
If you just want to have the count for a specific secondary usergroup, the following query (untested!) should also work:

SELECT COUNT(*) FROM user WHERE FIND_IN_SET(GROUPID, membergroupids);Replace GROUPID with the group you are searching for.