Jump to content

Recommended Posts

I'm not much good with php mysql queries with joins etc.

 

Here is my problem.

 

I have stored the results of my questionnaire in a table. The answers are multiple choice and have between 2 and 6 answers.

I need a query that returns the number of answers of each and the total number of answers.

 

I could find the number of people with each answer in a sepearate query for each answer:

 

SELECT Count(*) FROM answers

SELECT Count(*) FROM answers WHERE A1 = '100'

SELECT Count(*) FROM answers WHERE A1 = '75'

SELECT Count(*) FROM answers WHERE A1 = '50'

SELECT Count(*) FROM answers WHERE A1 = '0'

 

Then would need to repeat for A2, A3, B1, B2 etc each with different possible answers

 

How could I make that into one query for each question? Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/136528-solved-query-help/
Share on other sites

Wont that return a total number of people who answered?

 

I need seperatly the number of people who answered 100, the number of people who answered 75 etc

 

and thanks for tip about the *

 

Haha, you never said that (or did you?)... anyway... thats what GROUP BY is for ;-)

 

SELECT COUNT('A1') FROM `answers` WHERE `A1` = '50' OR `A1` = '75' OR `A1` = '100' GROUP BY `A1`;

 

Try that.

 

Kind regards,

Scott

Link to comment
https://forums.phpfreaks.com/topic/136528-solved-query-help/#findComment-712663
Share on other sites

Wont that return a total number of people who answered?

 

I need seperatly the number of people who answered 100, the number of people who answered 75 etc

 

and thanks for tip about the *

 

Haha, you never said that (or did you?)... anyway... thats what GROUP BY is for ;-)

 

SELECT COUNT('A1') FROM `answers` WHERE `A1` = '50' OR `A1` = '75' OR `A1` = '100' GROUP BY `A1`;

 

Try that.

 

Kind regards,

Scott

 

I said the number of answers of each, but i guess I wasnt very clear :)

 

I tried that, but I dont think it will work as:

 

it doesnt return empty rows for the options where answer to count = 0 and it doesnt label them.

 

e.g.:

SELECT COUNT('A1') FROM `answer` WHERE `A1` = '50' OR `A1` = '75' OR `A1` = '100' GROUP BY A1

gives: 

COUNT( 'A1' )
2
2

Link to comment
https://forums.phpfreaks.com/topic/136528-solved-query-help/#findComment-712665
Share on other sites

Wont that return a total number of people who answered?

 

I need seperatly the number of people who answered 100, the number of people who answered 75 etc

 

and thanks for tip about the *

 

Haha, you never said that (or did you?)... anyway... thats what GROUP BY is for ;-)

 

SELECT COUNT('A1') FROM `answers` WHERE `A1` = '50' OR `A1` = '75' OR `A1` = '100' GROUP BY `A1`;

 

Try that.

 

Kind regards,

Scott

 

I said the number of answers of each, but i guess I wasnt very clear :)

 

I tried that, but I dont think it will work as:

 

it doesnt return empty rows for the options where answer to count = 0 and it doesnt label them.

 

e.g.:

SELECT COUNT('A1') FROM `answer` WHERE `A1` = '50' OR `A1` = '75' OR `A1` = '100' GROUP BY A1

gives: 

COUNT( 'A1' )
2
2

 

Oh... this will return data showing which ones have X amount

 

SELECT `A1`, COUNT('A1') FROM `answer` WHERE `A1` = '50' OR `A1` = '75' OR `A1` = '100' GROUP BY `A1`;

 

If it don't show results with 0, then just make a php script to sort that out ;-)

 

Kind regards,

Scott

Link to comment
https://forums.phpfreaks.com/topic/136528-solved-query-help/#findComment-712667
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.