可能重复:
MYSQL COUNT GROUP BY 问题
$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()。原因是我需要找出表中每个用户的计数,即 adam 出现了多少次,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;
}