0

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.

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by dottomm
1

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
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.