为每个不同的行选择计数(mysql和php) [英] Select count for each distinct row (mysql and php)
本文介绍了为每个不同的行选择计数(mysql和php)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用mysql和php.
I am using mysql and php.
我有一张只有一栏的表格.我可以通过以下方式显示唯一行:
I have a table with one column. I can show unique rows by:
select distinct id from id_table
这可能会显示
1
2
3
5
我想做的是显示数字1、2等.
What I want to do is show the number of 1's, 2's, etc there are.
我能做
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
数据
1, 1, 1, 2, 3, 3, 5
结果
3, 1, 2, 1
谢谢
推荐答案
select id, count(id)
from table
group by id
如果只想计数id,那么
If only want count of ids, then
select count(id)
from table
group by id
这是有关分组依据子句的简单教程
Here is a simple tutorial of group by clause
http://www.w3schools.com/sql/sql_groupby.asp
这篇关于为每个不同的行选择计数(mysql和php)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文