18

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

1 Answer 1

29
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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.