Page 1 of 1

Counting Different Values in a Column

Posted: Fri Apr 16, 2010 10:07 am
by marcdd2
I have a column, X, with three possible values 0, 1, 2. I can easily count the number of rows with 0 in them by using a basic WHERE statement in my SELECT, but then if I want to count the numbers of 1’s and then 2’s I have to do two more SELECTS. Is there a more efficient way to SELECT the whole table and then count the rows containing the specific values from that SELECT? Thanks.

Re: Counting Different Values in a Column

Posted: Fri Apr 16, 2010 10:15 am
by pickle
The COUNT() and GROUP BY might be helpful. Something like this perhaps?

Code: Select all

SELECT
  COUNT(`x`)
FROM
  `yourTable`
GROUP BY
  `x`
I don't think this'll work actually, but it's close.

Re: Counting Different Values in a Column

Posted: Fri Apr 16, 2010 10:17 am
by infolock
you could use the mysql function count() and also group by


edit: heh, pickle, you're just too quick ;)