Can I do this in one Mysql query?

Posted by bsandrabr on Stack Overflow See other posts from Stack Overflow or by bsandrabr
Published on 2010-04-02T22:09:39Z Indexed on 2010/04/02 22:13 UTC
Read the original article Hit count: 281

Filed under:
|
|

Hi I have a table with two columns:

   column A    column B
     1             2
     1             2 
     2             1

I want to return total of ones = 3 total of twos = 3

The best I can come up with is two queries like so:

SELECT sum( 
CASE WHEN columnA =1 THEN 1  ELSE 0  END ) 
+ sum(CASE WHEN columnB =1 THEN 1 ELSE 0 END ) 

SELECT sum( 
CASE WHEN columnA =2 THEN 1  ELSE 0  END ) 
+ sum(CASE WHEN columnB =2 THEN 1 ELSE 0 END ) 

Can this be done in one query? Thanks

© Stack Overflow or respective owner

Related posts about mysql

Related posts about php