MySQL: Count two things in one query?

Posted by Nebs on Stack Overflow See other posts from Stack Overflow or by Nebs
Published on 2010-04-20T14:41:19Z Indexed on 2010/04/20 14:43 UTC
Read the original article Hit count: 292

Filed under:
|
|

I have a "boolean" column in one of my tables (value is either 0 or 1).

I need to get two counts: The number of rows that have the boolean set to 0 and the number of rows that have it set to 1. Currently I have two queries: One to count the 1's and the other to count the 0's.

Is MySQL traversing the entire table when counting rows with a WHERE condition? I'm wondering if there's a single query that would allow two counters based on different conditions?

Or is there a way to get the total count along side the WHERE conditioned count? This would be enough as I'd only have to subtract one count from the other (due to the boolean nature of the column). There are no NULL values.

Thanks.

© Stack Overflow or respective owner

Related posts about mysql

Related posts about php