mysql - rows are multipied because of joining table? -
i querying "questions" , joining "answers". each question may have 2 or more answers. due which, number of questions returned being multiplied number of answers. prevents me implementing pagination. how can avoid this? thanks.
here query:
<cfquery name="questions"> select questions.id questionid, questions.question question, questions.rank rank, questions.isrequired isrequired, questiontypes.id questiontypeid, questiontypes.name questiontype, questiontypes.template template, questions.survey_id survey_id, surveys.name surveyname, surveys.questionsperpage questionsperpage, surveys.thankyoumsg thankyoumsg, answers.id answerid, answers.answer answer questions left join answers on answers.question_id = questions.id inner join questiontypes on questions.questiontype_id = questiontypes.id inner join surveys on questions.survey_id = surveys.id questions.survey_id = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.surveyid#"> </cfquery>
you can ids , answers in comma-delimited lists using group_concat():
select questions.id questionid, questions.question question, questions.rank rank, questions.isrequired isrequired, questiontypes.id questiontypeid, questiontypes.name questiontype, questiontypes.template template, questions.survey_id survey_id, surveys.name surveyname, surveys.questionsperpage questionsperpage, surveys.thankyoumsg thankyoumsg, group_concat(answers.id) answerids, group_concat(answers.answer) answers questions left join answers on answers.question_id = questions.id inner join questiontypes on questions.questiontype_id = questiontypes.id inner join surveys on questions.survey_id = surveys.id questions.survey_id = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.surveyid#"> group questions.id
Comments
Post a Comment