1

各管理者ロールのユーザーの総数を取得して出力するための構文を知る必要があります。

Roles
id  |  name
---    ----
1   |  admin
2   |  moderator
3   |  user

表-2

users
id  |  name | role
--     ----   ----
1   |  a    |  1
2   |  b    |  2
3   |  c    |  2
4   |  d    |  2
5   |  e    |  3
6   |  f    |  3
7   |  g    |  3
8   |  h    |  1
9   |  i    |  1

コード

 <?php
   $sql = "select r.name, r.id from role r, users u where r.id=u.role";
   $result = mysql_query($sql);
   $count = 0;
   while($row = mysql_fetch_array($result))
    {
      if($count!=$row['1'])
        {
          echo $row['0']."- NEED TOTAL NUMBER OF USERS ON EACH ROLES TO BE PRINTED HERE.";
          $count=$row['1'];
        }
?>
4

4 に答える 4

3
SELECT  COUNT(b.role)
FROM    roles a
        LEFT JOIN  users b
            ON a.ID = b.role
WHERE   a.name = 'admin'

更新 1

SELECT  a.name, COUNT(b.role) totalCount
FROM    roles a
        LEFT JOIN  users b
            ON a.ID = b.role
GROUP BY a.Name
于 2012-11-10T12:14:18.770 に答える
2

group by 句が必要です:)

SELECT a.name, COUNT(b.id) "Total num of Users"
FROM    roles a, users b
        WHERE a.ID = b.role
group by a.ID,b.role
于 2012-11-10T12:30:47.887 に答える
1
$result=mysql_fetch_array(mysql_query('select count(*) as total from users where role="1" '));
$result = $result['total'];
echo "Total Admins: ".$result; 

それは今動作します!

于 2012-11-10T13:05:34.873 に答える
0

それはあなたを助けるかもしれません:

<?php
$sql = "select r.name, r.id, count(u.role) as ct from role r, users u where r.id=u.role group by u.role";
$result = mysql_query($sql);
$count = 0;
while($row = mysql_fetch_array($result))
{
    if($row['2']==0)
    {
        echo $row['0']."- NEED TOTAL NUMBER OF USERS ON EACH ROLES";
    }
?>
于 2012-11-10T12:28:06.743 に答える