Hello.

I am having trouble with a count col query I hope some one can help me with.

I have six questions and I would like to count how many "yes" answers in each col and then group by that col.
Here is my schema
table:survey

||id || Q1|| Q2|| Q3||Q4 ||Q5 ||Q6||
___________________________
||1 || yes || yes|| yes||no||yes||no
||2 || no || yes|| no||no||yes|| yes
||3 || no || yes|| no||no||yes|| yes

So essentially I would like 6 select queries but I can't figure out how to do this.

$result=mysql_query("SELECT Q1, count(*) as Q1_count FROM survey where Q1='yes' GROUP BY Q1");

How can I do this for all six columns and get a "count_alias" for each col? and can it be done with a single query?

thanks in advance to anyone who can help.

Recommended Answers

All 2 Replies

Are you looking for this?

SELECT SUM(IF(Q1='yes', 1, 0)) AS Q1_count, SUM(IF(Q2='yes', 1, 0)) Q2_count 
FROM survey

Yes! That is exactly what I was looking for. Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.