为每个不同的行选择计数(mysql 和 php)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8264658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select count for each distinct row (mysql and php)
提问by Jason Pawlak
I am using mysql and php.
我正在使用 mysql 和 php。
I have a table with one column. I can show unique rows by:
我有一张只有一列的表格。我可以通过以下方式显示独特的行:
select distinct id from id_table
This might show
这可能显示
1
2
3
5
What I want to do is show the number of 1's, 2's, etc there are.
我想要做的是显示 1、2 等的数量。
I could do
我可以
select count(id) from id_table where id = 1
But then I have to run a query for every... single... row... And with hundreds of thousands of rows, not good.
但是然后我必须为每个......单......行运行一个查询......并且有数十万行,不好。
Is there a way to return an array of the counts of each distinct item
有没有办法返回每个不同项目的计数数组
Data
数据
1, 1, 1, 2, 3, 3, 5
Results
结果
3, 1, 2, 1
Thanks
谢谢
回答by Zohaib
select id, count(id)
from table
group by id
If only want count of ids, then
如果只想要 ids 的数量,那么
select count(id)
from table
group by id
Here is a simple tutorial of group by clause
这是 group by 子句的简单教程