计算表中的每个用户名

时间:2012-09-26 17:54:22

标签: php arrays

  

可能重复:
  MYSQL COUNT GROUP BY question

enter image description here

$query = "SELECT * FROM product_id_1";

$result = mysql_query($query);

while( $record = mysql_fetch_array($result) ){

$array[] = $record['username'];

$unique_array = array_unique($array);

}


foreach( $unique_array as $list ){
    echo $list.'<br/>';
}

问候语,

我一直试图弄清楚如何应用array_count_unique()。原因是我需要找出表中每个用户的计数,即亚当出现的次数和abcd出现的次数。


更新

根据你们的建议,这就是我所做的:

$query = 'SELECT username, COUNT(*) AS username_count FROM product_id_1 GROUP BY username';

$result = mysql_query($query);

while ($record = mysql_fetch_object($result)) {

    echo $record->username;

    echo $record->username_count;
}

1 个答案:

答案 0 :(得分:3)

这正是我们拥有RDBMS和SQL的原因:

SELECT COUNT(1) AS username_count, username
FROM
    product_id_1
GROUP BY
    username