gpt4 book ai didi

PHP/mySQL 计算每个国家代码的总评论数

转载 作者:太空宇宙 更新时间:2023-11-03 12:11:30 25 4
gpt4 key购买 nike

我在 mySQL 中有三个表 - 类别、主题和评论。我正在尝试计算对某个主题的评论总数,并按总数对每个国家/地区代码进行分组。目前有几个国家代码在评论表的 comments_location 中多次出现,其中包含不同的国家代码(例如 US、UK、CH、ETC)。但是,这些国家代码可以在 comments_location 中出现多次。我想对 comments_location 进行分组(例如,comments_location 有 3 个英国记录、2 个美国记录等 - 而不是出现 3 次,我想对 comments_location(按单个英国、美国、等)进行分组并显示每个国家的记录总数)。下面是一个尝试示例,但它不起作用:

// should there be a FOR statement and where should it be placed?
//query comments where comment location = (US,UK,ETC.)
$sqlcomment = mysql_query("SELECT * FROM comments WHERE comments_location='NEED TO RUN THROUGH EACH COUNTRY CODE AND GROUP e.g. (GROUP US - run count, then GROUP UK - run count, ETC)'");
//get comment total (run count)
$commentnumber = mysql_num_rows($sqlcomment);
//echo each variable into a string one by one (should the FOR be placed here?)
$variable1 = "CODE: NEED VARIABLE FOR COMMENTS_LOCATION, value: $commentnumber, ";
echo $variable1;

理想情况下,echo $variable1 应该显示 CODE: US, value: 2240, CODE: UK, value: 6240, CODE: CH, value: 3240,等等继续循环,直到列出所有可能的国家代码。

我是否需要存储所有可能的国家/地区代码的数组并将它们放在 $sqlcomment 中 comments_location 的位置?

最佳答案

请尝试以下查询:

$sqlcomment = mysql_query("SELECT comments_location,count(comments_id)as count FROM comments GROUP BY comments_location");
echo'<pre>';
while($row=mysql_fetch_array($sqlcomment)){
print_r($row);
}
echo'</pre>';

comments_id 是一个猜测,需要时进行调整。

这应该会给您 n 个结果,而 n=Count of the Countries。你将在每一行然后是国家 + 计数。

关于PHP/mySQL 计算每个国家代码的总评论数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24019058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com