繁体   English   中英

PHP MySQL计数行使用groupby不起作用?

[英]Php mysql count row using groupby not working?

PHP MySQL计数行使用groupby不工作?

表格:test_table

__________________________
| id | name |    note    |
| 1  | aaa  |  234234234 |
| 2  | aaa  |  kjhkjhkjj |
| 3  | bbb  |  jhghjgjjj |
| 4  | bbb  |  uiyiuyutt |

..............

<?PHP
        $result = mysql_query("SELECT COUNT(*) FROM `test_table` GROUP BY name");
        $row = mysql_fetch_row($result);
        mysql_free_result($result);
        $row_result = $row[0];
        echo $row_result;
?>

当我测试此代码时,它回显1

我想得到结果2我该怎么办?

您应该循环结果:

   $result = mysql_query("SELECT name, COUNT(id) cnt FROM `test_table` GROUP BY name");
    while ($row = mysql_fetch_assoc($result)) {
        echo $row['name'].' : '.$row['cnt'];
    }
    mysql_free_result($result);

并且可以肯定,您应该停止使用mysql_函数了很长一段时间。

mysql不再使用,请使用mysqlipdo

 <?PHP
$result = mysql_query("SELECT COUNT(name) FROM `test_table` GROUP BY name");
if ($result) {
    while ($row = mysql_fetch_row($result)) {
        $row_result = $row[0];
        echo $row_result;
    }
    mysql_free_result($result);
}
?> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM