简体   繁体   中英

Select count for each distinct row (mysql and 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

This might show

1
2
3
5

What I want to do is show the number of 1's, 2's, etc there are.

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

select id, count(id)
from table
group by 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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