Page 1 of 1

SELECT COUNT(UNIQUE(column)) ?????

Posted: Fri Mar 06, 2009 11:51 am
by Skara
This is maybe a little hard to explain..
I have many many entries with an (int)`status` column. I want to know if I can select the count of different types of statuses. In other words...
The entries are: 1,6,2,4,3,2,3,2,4,4,5,5,7,8,7
I want the results to be:
(1,1)
(6,1)
(2,3)
(4,3)
(3,2)
(5,2)
(7,2)
(8,1)
-- I think I counted those right. :P

In other words, I don't want to have to select ALL the status rows and add them up myself.
(current solution:)

Code: Select all

SELECT `status`;
loop {
    $status[`status`]++;
}
better solution?:
I want to know "How many of each of the different status values are there?"

Re: SELECT COUNT(UNIQUE(column)) ?????

Posted: Fri Mar 06, 2009 1:21 pm
by Eran

Code: Select all

SELECT `status`,COUNT(`id`) AS count FROM `table_name` GROUP BY `status`
Replace 'id' with your primary key name if it is different.

Re: SELECT COUNT(UNIQUE(column)) ?????

Posted: Sun Mar 08, 2009 9:32 am
by Weirdan
I want to know "How many of each of the different status values are there?"
If you only want a count (but not the specific statuses available) you may want to use a simpler query:
[sql] SELECT count(DISTINCT `status`) FROM `table_name` [/sql]

Re: SELECT COUNT(UNIQUE(column)) ?????

Posted: Mon Mar 09, 2009 10:04 am
by Skara
Aha... yeah, the first reply is what I need. Thanks, both!!