dongzuoyue6556
2013-12-01 20:01如何使用mysql和php计算每个州的总条目数
I have a table "data" with a column "name" "state" and a few more
name state
peter MN
john NY
jay NY
sam CO
jack TX
jill NO
I want to calculate the number of entries per state and want my output as follows for example:
NY: 125
MN: 21
CO: 17
TX: 10
NO: 59
etc...
I have a query like this
$stmt = $db->query("SELECT state, COUNT(*) FROM `data` GROUP BY state;");
$nums = $stmt->rowCount();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
echo "<tr>
<td>" . $row["state"] . "</td>
<td>$nums</td>
</tr>";
}
This displays every state in my table but does not return the corresponding number of entries for that state. This only returns the number of states i.e. 50. How can I display the number of entries per state?
- 点赞
- 回答
- 收藏
- 复制链接分享
2条回答
为你推荐
- Laravel Relationship并在结果集中为每个条目提供前几个条目
- laravel
- mysql
- php
- 1个回答
- PHP:检索两个最新的mySQL表条目并将每个条目存储在不同的变量中
- variables
- mysql
- php
- 1个回答
- 使MySQL每个条目只返回一行
- mysql
- php
- 2个回答
- 设置MySQL表中每个userid的最大条目数
- sql
- mysql
- php
- 2个回答
- 如何使用mysql和php计算每个州的总条目数
- mysql
- php
- 2个回答
换一换