php - How to select multiple fields in the same row? (MySQL) -


I'm about to use MySQL for matching results for a survey.

Now my question is, if I had a host of survey questions, a table of survey options, and a host of users in response to those survey questions, I would ask that all of the survey How do I choose a survey question with options?

question table

  question_id (int) question (text)  

option table

  Choice_id (int) option_id (int) option_text (varchar)  

answer table

  answer_id (int) question_id (int) choice_id (int)  

In the same question, what should I do to get survey questions with all the options of that survey (known or unknown amount)? (If possible, do math too, in my second question, within the same question)

I am not so advanced with MySQL.

Thanks

Edit: Sorry, what I meant, I am trying to get a SELECT statement to choose the question, and All options in line with that question are all options.

I select something like <

  question_id, question, choice_id, using option_text LEFT option by JOIN (question_id)  

I get multiple rows, do one, then for each choice_id

the result should be something like

  question option_1 choice_2 option_3a or b or cabc  

Part of math is the result of the survey, and yes, like_id is PK, if it helps.

questions to get and options for each question:

  Select question, ask option_text from question but questions.question_id = options.question_id  options 

join before the LEFT pair if you also want to have questions that do not have any options .

You can calculate for each answer:

  SELECT question, choice_text, COUNT (answers.choice_id) Questions on question by: questions.question_id = choices.question_id on the left Questions.question_id = answers.question_id and choices.choice_id = answers.choice_id Join Group Answers questions.question_id, by questions.question_id by options.choice_id order, join the c options Hoices.choice_id  
< P> Use the following question (per question) to get the number of people who choose each answer as a percentage:

  f Do not ask questions, choice_text, COUNT (answers.choice_id) * 100 / Questiontotal questions (select questions.question_id, COUNT) (answers.choice_id) Join the LEFT answer questions as questiontotal at questions.question_id = answers. Questions_id by group questions.question_id) in the form of answercounts at questions.question_id = answercounts.question_id questions.question_id = choices.question_id on the left, questions.question_id = answers.question_id and choices.choice_id = answers.choice_id include group answers questions. Question_id, include options by questions.question_id with options.choice_id command, Options.choice_id;  

The test data used here is:

  create table questions (Question_ID int, question nvarchar (100)); Enter questions in question (question_ id, question) value (1, 'fu?'), (2, 'bar?'); Select table options (like_id int, question_id int, choice_text nvarchar (100)); Enter the option (choice_id, question_id, option_text) in the value (1, 1, 'Foo1'), (2, 1, 'foo2'), (3, 1, 'Foo3'), (4, 2, 'Bar1') , (5, 2, 'bar2'); Create table answer (answer_id int, question_id int, choice_id int); Answer the values ​​(answer_id, question_id, like_add) value (1, 1, 1), (2, 1, 1), (3, 1, 3), (4, 2, 4), (4, 2, 5) );  

And the output I get with the last query on this data:

  'foo' ',' fu1 ', 66.6667' foo ' 'Foo2', 0.0000 'Foo', 'Foo 3', '33.3333' times? ',' Bar 1 ', 50.0000' times? ',' 2 times', '50.0000', the question that you say is that you want to return all the questions in one line in a row, I suggest that you  no  Try to do this, and instead I use the above method if you need to present data in a line in your end-user, then it can be done using PHP . 


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -