Got the following tables:
forms
- id
- name
questions
- id
- form_id
- type
- name
questions_translations
- id
- question_id
- lang_id
- label
- placeholder
poss_answers
- id
- question_id
- lang_id
poss_answers_translations
- id
- poss_answer_id
- value
Now I want to select all the questions and possible answers from 1 form, from 1 language. I can easily select all the elements without using the poss_answers_table:
SELECT b.*, f.* FROM app_questions q JOIN app_questions_translations qt ON q.form_id = 4 WHERE lang_id = 4 && qt.question_id = q.id
This gets me all the elements for the two tables, but now I want to add in the poss_answers table. This table holds all the possible answers for a select field, or a checkbox field, or radio field. These are the answers which can be choosen from.
But how can I do that within 1 query?
-
Just join the poss_answers table as well.Styphon– Styphon2015年07月17日 10:59:48 +00:00Commented Jul 17, 2015 at 10:59
1 Answer 1
Presuming that 1 question (row) has many answers (rows), joining to the answer table will give you the question details returned for every answer row. This is not ideal.
You need to do a 'GetAnswers' query, inside your 'GetQuestions' query, to return all the answers for a particular question.
Something like this..
//'GetQuestions'
foreach($questions as $question)..
echo $question;
//'GetAnswers - for Question;
foreach($answers as $answer)..
echo $answer;
//endAnswerLoop
//endQuestionLoop