您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何使用array_count_values()和mySQL计数值出现

如何使用array_count_values()和mySQL计数值出现

您需要GROUP BY和COUNT():

SELECT image, COUNT(*) as Votes FROM january GROUP BY image

这将返回两列:1表示图像,1表示该图像的投票数:

image     Votes
001.jpg   1
002.jpg   32
...

完整代码

$db = MysqL_connect("xxx", "xxx", "xxx");
MysqL_select_db("club",$db);
$q = MysqL_query("SELECT image, COUNT(*) as Votes FROM january GROUP BY image");

$Votes = array();
while ($row = MysqL_fetch_assoc($q)) {
    $Votes[$row['image']] = $row['Votes'];
}

// $Votes is an array of 'image' => 'Votes'

foreach($Votes as $image => $count) {
    echo "$image: $count<br>";
}
MySQL 2022/1/1 18:53:08 有375人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶