ARAVINDH GOPI ARAVINDH GOPI - 4 days ago 5
MySQL Question

How to count field values of multiple rows

I have a table like the one in the picture, I want to select all '6' in m1,m2,m3,m4.m5 field where name is equal to sivakumar.

enter image description here

I have tried

Select m1,m2,m3,m4,m5 from table where m1=6 and m2=6 and so on... where staff_name=sivakumar

Answer

Sorry for the inconvienience, my question is "how to count field values of multiple rows". I got my answer below

select (sum(case when m1 = 6 then 1 else 0 end) + 
sum(case when m2 = 6 then 1 else 0 end) + 
sum(case when m3 = 6 then 1 else 0 end) + 
sum(case when m4 = 6 then 1 else 0 end) +
 sum(case when m5 = 6 then 1 else 0 end)) as abc from table where staff_name='sivakumar';
Comments