由于连接表,行相乘?

时间:2013-09-04 14:26:42

标签: mysql coldfusion

我正在查询“问题”并加入“答案”。每个问题可能有两个或更多答案。因此,返回的问题数量乘以答案数量。这使我无法实施分页。我怎么能避免这个?感谢。

这是我的问题:

<cfquery name="questions">
    SELECT
       questions.id as questionid,
       questions.question as question,
       questions.rank as rank,
       questions.isrequired as isrequired,
       questiontypes.id AS questiontypeid,
       questiontypes.name as questiontype,
       questiontypes.template as template,
       questions.survey_id as survey_id,
       surveys.name as surveyname,
       surveys.questionsperpage as questionsperpage,
       surveys.thankyoumsg as thankyoumsg,
       answers.id as answerid,
       answers.answer as answer
    FROM
       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
   WHERE
   questions.survey_id = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.surveyid#">  
</cfquery>

3 个答案:

答案 0 :(得分:2)

您可以使用group_concat()在逗号分隔的列表中获取ID和答案:

SELECT
       questions.id as questionid,
       questions.question as question,
       questions.rank as rank,
       questions.isrequired as isrequired,
       questiontypes.id AS questiontypeid,
       questiontypes.name as questiontype,
       questiontypes.template as template,
       questions.survey_id as survey_id,
       surveys.name as surveyname,
       surveys.questionsperpage as questionsperpage,
       surveys.thankyoumsg as thankyoumsg,
       group_concat(answers.id) as answerids,
       group_concat(answers.answer) as answers
    FROM
       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
   WHERE
   questions.survey_id = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.surveyid#">
   group by questions.id

答案 1 :(得分:2)

这更像是一个澄清,但正如我在评论中提到的,Steve和Brad建议的替代方案与the "list approach"不同,因为它们需要两个查询:一个用于检索要在当前页面上显示的问题的ID,以及用于获取相关答案的ID。

有多种方法可以实现它,但有一种方法是使用“分组”输出as Steve mentioned。假设您每页显示10条记录,并希望通过11显示结果20。首先运行查询以获取问题ID。

    <!---  For clarity these are descriptive, rather than actual, column names --->
    <cfquery name="getCurrentPageQuestions" .....>
        SELECT  QuestionID
        FROM    Questions
        WHERE   SurveyID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.surveyid#"> 
        ORDER BY WhateverColumnsYouWant
        LIMIT  11, 20
    </cfquery>

接下来,使用这些id来过滤原始的JOIN。您可以跳过调查ID上的过滤器,因为第一个查询处理了这个问题。

重要提示:您必须首先按分组列对结果进行排序。否则,分组的cfoutput将无效。

    <cfquery name="getAnswers" .....>
        SELECT
                q.QuestionID
                , q.Question
                , a.AnswerID
                , a.Answer
                , .....
        FROM    questions q
                    LEFT JOIN answers a ON a.questionID = q.questionID
                    INNER JOIN questiontypes qt ON qt.questionTypeID = q.questionTypeID
                    INNER JOIN surveys s ON s.surveyID = q.surveyID
        WHERE  q.questionID IN 
               (
                   <cfqueryparam value="#valueList(getCurrentPageQuestions.questionID)#" 
                        list="true" 
                        cfsqltype="cf_sql_integer">
               )
        ORDER BY q.questionID
    </cfquery>

最后,使用Steve的示例以正确的格式显示结果:

    <cfouput query="getAnswers" group="questionID">
         <!--- display question once --->
        #question#<br>

       <cfoutput> 
            <!--- display all answers --->
             - #answer#<br>
        </cfoutput>
    </cfoutput>

答案 2 :(得分:1)

因为这是标记为ColdFusion。 我想提出一个ColdFusion优雅的解决方案,更容易处理逗号加入列表。

如果您使用&lt; cfoutput&gt;标签,带属性查询和组 您可以使用1个问题和x个答案分层输出数据。

<cfouput query="questions" group="questionID">
    #question#<br>
    <cfoutput>
    - #answer#<br>
    </cfoutput>
</cfoutput>

您可以在div或表行中设置此输出的样式,并使用问题/答案类将它们相互缩进。

关于“阻止我实施分页”这方面的问题 剥皮的方法太多了。我建议研究一些ajax。 在用户点击“显示答案”

时,将问题分页并获取答案