Counting Different Values in a Column
Moderator: General Moderators
Counting Different Values in a Column
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
The COUNT() and GROUP BY might be helpful. Something like this perhaps?
I don't think this'll work actually, but it's close.
Code: Select all
SELECT
COUNT(`x`)
FROM
`yourTable`
GROUP BY
`x`Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Re: Counting Different Values in a Column
you could use the mysql function count() and also group by
edit: heh, pickle, you're just too quick
edit: heh, pickle, you're just too quick