0

我在下面有一个“答案”数据库表:

答案表

AnswerId SessionId QuestionId Answer
13       AAA       1          A
14       AAC       1          True

现在您可以看到,考试(会议)AAA 中的问题 1 有 1 个答案,而这些是考试(会议)AAC 中问题 1 的 1 个答案。

下面是“问题”表:

问题表:

SessionId  QuestionId QuestionContent NoofAnswers ReplyId QuestionMarks OptionId
AAA        1          What is 2+2?    1           1       5             2
AAC        1          Is 3+3 = 6?     1           1       5             25

现在我有一个搜索功能,用户可以从问题中输入一个术语并编译搜索。因此,例如,如果用户输入“2+2”,则结果应显示在 php/html 表中:

   QuestionContent Option Type  Number of Answers Answer Number of Replies  Number of Marks
   What is 2+2?    A-D          1                 A      Single             5

但问题是它显示了一个额外的行,它在下面显示:

   QuestionContent Option Type  Number of Answers Answer Number of Replies  Number of Marks
   What is 2+2?    A-D          1                 A True    Single             5
   What is 2+2?    A-D          1                 A True    Single             5

现在我的问题是,为什么它显示两行,为什么它在“答案”列下包含答案“真”,而该问题的答案应该只是“A”?我相信这是因为这两个问题具有相同的 QuestionId(问题编号),但它们都属于不同的 SessionId,所以这应该不是问题。

下面是执行查询并输出结果的代码(我已将其简化,以便您更容易阅读并希望看到问题):

<?php

//connect to db


    // Build the query
    $questionquery = "
SELECT DISTINCT q.QuestionContent, o.OptionType, q.NoofAnswers, GROUP_CONCAT(DISTINCT Answer SEPARATOR '') AS Answer, r.ReplyType, 
       q.QuestionMarks 
  FROM Answer an 
  INNER JOIN Question q ON q.QuestionId = an.QuestionId
  JOIN Reply r ON q.ReplyId = r.ReplyId 
  JOIN Option_Table o ON q.OptionId = o.OptionId 
      WHERE ".implode(" AND ", array_fill(0, $numTerms, "q.QuestionContent LIKE ?"))."
      GROUP BY an.SessionId, an.QuestionId
      ORDER BY ".implode(", ", array_fill(0, $numTerms, "IF(q.QuestionContent LIKE ?, 1, 0) DESC"))."
    ";

    // Make the referenced array
    $referencedArray = make_values_referenced(array_merge(
      array(str_repeat("ss", $numTerms)), // types
      $termArray,                         // where
      $termArray                          // order by
    ));


    // Bind parameters
    if (!call_user_func_array(array($stmt, 'bind_param'), make_values_referenced($referencedArray))) {
      die("Error binding parameters: $stmt->error"); 
    }


    // This will hold the search results
    $searchResults = array();
    $searchOption = array();
    $searchNoofAnswers = array();
    $searchAnswer = array();
    $searchReply = array();
    $searchMarks = array();

    // Fetch the results into an array
    if (!$stmt->num_rows()) {
      $stmt->bind_result($dbQuestionContent,$dbOptionType,$dbNoofAnswers,$dbAnswer,$dbReplyType,$dbQuestionMarks); 
      while ($stmt->fetch()) {
        $searchResults[] = $dbQuestionContent;
        $searchOption[] = $dbOptionType;
        $searchNoofAnswers[] = $dbNoofAnswers;
        $searchAnswer[] = $dbAnswer;
        $searchReply[] = $dbReplyType;
        $searchMarks[] = $dbQuestionMarks;
      }
    }

  }


      $questionnum = sizeof($searchResults);

    // If $searchResults is not empty we got results
    if (!empty($searchResults)) {
      echo "<p>Your Search: '$inputValue'</p>";  
      echo"<p>Number of Questions Shown from the Search: <strong>$questionnum</strong></p>";
      echo "<table border='1' id='resulttbl'>
      <tr>
      <th class='questionth'>Question</th>
      <th class='optiontypeth'>Option Type</th>
      <th class='noofanswersth'>Number of <br/> Answers</th>
      <th class='answerth'>Answer</th>
      <th class='noofrepliesth'>Number of <br/> Replies</th>
      <th class='noofmarksth'>Number of <br/> Marks</th>
      </tr>\n";
      $script = '';
      foreach ($searchResults as $key=>$question) {
      $script .= 'var key_' . $key . '="' . str_replace('"','\"', $question) . '";' . PHP_EOL;
        echo '<tr class="questiontd">'.PHP_EOL;
        echo '<td>'.htmlspecialchars($question).'</td>' . PHP_EOL;
        echo '<td class="optiontypetd">'.htmlspecialchars($searchOption[$key]).'</td>' . PHP_EOL;
        echo '<td class="noofanswerstd">'.htmlspecialchars($searchNoofAnswers[$key]).'</td>' . PHP_EOL;
        echo '<td class="answertd">'.htmlspecialchars(implode(' ', $searchAnswer)).'</td>' . PHP_EOL; 
        echo '<td class="noofrepliestd">'.htmlspecialchars($searchReply[$key]).'</td>' . PHP_EOL;
        echo '<td class="noofmarkstd">'.htmlspecialchars($searchMarks[$key]).'</td>' . PHP_EOL;
        echo "<td class='addtd'><button type='button' class='add' onclick=\"parent.addwindow(key_$key,'$searchMarks[$key]','$searchNoofAnswers[$key]','$searchOption[$key]','$searchReply[$key]','$searchAnswer[$key]');\">Add</button></td></tr>";
}
        echo "</table>" . PHP_EOL;
        echo '<script type="text/javascript">' . PHP_EOL;
        echo $script;
        echo '</script>' . PHP_EOL;


}
?>
4

2 回答 2

1

这里的问题似乎与您定义的数据库架构有关,而不是您使用它的方式,我的意思是答案表上的唯一值似乎应该是 answerID 或 SessionId+QuestionId,所以如果您在查询中使用该表,您还应该包括SessionId 在您与 Question 表的连接条件下,我的意思是至少看起来不同会话中的两个不同问题可以有相同的 questionID 是关键问题。

因此,您的代码应如下所示:

SELECT DISTINCT q.QuestionContent, o.OptionType, q.NoofAnswers, GROUP_CONCAT(DISTINCT     Answer SEPARATOR '') AS Answer, r.ReplyType, 
       q.QuestionMarks, q.SessionId
FROM Answer an 
INNER JOIN Question q ON q.QuestionId = an.QuestionId and an.SessionId = q.SessionId
JOIN Reply r ON q.ReplyId = r.ReplyId 
JOIN Option_Table o ON q.OptionId = o.OptionId 
    WHERE ".implode(" AND ", array_fill(0, $numTerms, "q.QuestionContent LIKE ?"))."
    GROUP BY an.SessionId, an.QuestionId
    ORDER BY ".implode(", ", array_fill(0, $numTerms, "IF(q.QuestionContent LIKE ?, 1, 0) DESC"))."
";

UPDATED上面的 SQL 查询反映到更新的问题

新问题仍然与查询没有指定会话有关,因此跨会话的不同问题可能会混合在一起,我建议您在选择列表中查询 sessionID 以获取正确的数据,并将其隐藏在页面上如果您不想显示它,但请记住,如果您有一些其他详细信息页面来显示问题,您仍然需要将 sessionID 和 QuestionId 完全传递到该页面以识别唯一数据

于 2012-10-11T02:14:57.290 回答
0

包括an.QuestionId在选择查询中。

$questionquery = "
SELECT DISTINCT an.QuestionId,q.QuestionContent, o.OptionType, q.NoofAnswers, GROUP_CONCAT(DISTINCT Answer SEPARATOR '') AS Answer, r.ReplyType, 
   q.QuestionMarks 
FROM Answer an 
INNER JOIN Question q ON q.QuestionId = an.QuestionId
JOIN Reply r ON q.ReplyId = r.ReplyId 
JOIN Option_Table o ON q.OptionId = o.OptionId 
  WHERE ".implode(" AND ", array_fill(0, $numTerms, "q.QuestionContent LIKE ?"))."
  GROUP BY an.SessionId, an.QuestionId
  ORDER BY ".implode(", ", array_fill(0, $numTerms, "IF(q.QuestionContent LIKE ?, 1, 0)  DESC"))."
 ";

您已使用 group by foran.QuestionId但未将其包含在选择列中,因为即将出现多个数据。

于 2012-10-11T02:11:43.963 回答