我在下面的查询中有一个动态where子句,它根据所选的下拉菜单输出结果。现在,where子句在添加和删除某些条件时可以正常工作,具体取决于所选的选项。
但我有点意识到,如果我想查看所有问题,它没有QuestionId = ?
条件,这是好的,但是当假设显示每个问题的答案时,而不是像这样显示:
D (this is for question 1)
B,C,E (this is for question 2)
将它们全部显示为如下所示的一个答案:
D,B,C,E
我的问题是导致这个,查询或循环的原因是什么?
var_dump($ questions):显示:
array(1) { [39]=> array(1) { [72]=> array(1) { ["answer"]=> string(7) "B,C,D,E" } } }
72是QuestionId,所以出于一些奇怪的原因,它没有经过所有问题(应该有第二个问题是73)
以下是代码:
$selectedstudentanswerqry = "
SELECT sa.StudentId, q.QuestionId,
GROUP_CONCAT( DISTINCT Answer ORDER BY Answer SEPARATOR ',' ) AS Answer
FROM Student st
INNER JOIN Student_Answer sa ON (st.StudentId = sa.StudentId)
INNER JOIN Question q ON (sa.QuestionId = q.QuestionId)
INNER JOIN Answer an ON q.QuestionId = an.QuestionId
";
// Initially empty
$where = array('q.SessionId = ?');
$parameters = array($_POST["session"]);
$parameterTypes = 'i';
//check if POST is empty
// Check whether a specific student was selected
$p_student = empty($_POST["student"])?'':$_POST["student"];
switch($p_student){
case 0:
//dont' add where filters
break;
default:
$where[] = 'sa.StudentId = ?';
$parameters[] .= $_POST["student"];
$parameterTypes .= 'i';
}
// Check whether a specific question was selected
$p_question = empty($_POST["question"])?'':$_POST["question"];
switch($p_question){
case 0:
//dont' add where filters
break;
default:
$where[] = 'q.QuestionId = ?';
$parameters[] .= $_POST["question"];
$parameterTypes .= 'i';
}
// If we added to $where in any of the conditionals, we need a WHERE clause in
// our query
if(!empty($where)) {
$selectedstudentanswerqry .= ' WHERE ' . implode(' AND ', $where);
global $mysqli;
$selectedstudentanswerstmt=$mysqli->prepare($selectedstudentanswerqry);
// You only need to call bind_param once
if (count($where) == 1) {
$selectedstudentanswerstmt->bind_param($parameterTypes, $parameters[0]);
}
else if (count($where) == 2) {
$selectedstudentanswerstmt->bind_param($parameterTypes, $parameters[0], $parameters[1]);
}
else if (count($where) == 3) {
$selectedstudentanswerstmt->bind_param($parameterTypes, $parameters[0], $parameters[1], $parameters[2]);
}
}
$selectedstudentanswerqry .= "
GROUP BY sa.StudentId, q.QuestionId
ORDER BY StudentAlias, q.SessionId
";
// get result and assign variables (prefix with db)
$selectedstudentanswerstmt->execute();
$selectedstudentanswerstmt->bind_result($detailsStudentId,$detailsQuestionId,$detailsAnswer);
$selectedstudentanswerstmt->store_result();
$selectedstudentanswernum = $selectedstudentanswerstmt->num_rows();
$questions = array();
while ($selectedstudentanswerstmt->fetch()) {
$questions[$detailsStudentId][$detailsQuestionId] = array(
'answer'=>$detailsAnswer,
);
}
...
<?php
var_dump($questions);
foreach ($questions as $studentId => $student)
{
foreach ($student as $questionId => $question) {
echo '<p><strong>Answer:</strong> ' .htmlspecialchars($question['answer']). '</p>' . PHP_EOL;
}
}
?>
答案 0 :(得分:0)
看起来问题实际上是:
GROUP_CONCAT( DISTINCT Answer ORDER BY Answer SEPARATOR ',' ) AS Answer
查询的一部分。如果没有看到实际的表结构和数据,就很难形成正在发生的事情的心理形象。 “答案”可能是一个模糊的列,因此会导致不可预测的结果。
您可以考虑编写子查询或为此创建视图。
SELECT sa.StudentID, q.QuestionId, sa.Student_Answers
FROM (
SELECT GROUP_CONCAT(DISTINCT a.Answer SEPERATOR ',') as Question_Answers
FROM Student_Answer a
GROUP BY a.QuestionID
) AS sa
INNER JOIN Question q ON (sa.QuestionId = q.QuestionId)
查看您的代码,似乎没有必要加入Students表,因为他们的答案中已经存在ID。虽然我确信在某些时候你会将名字与ID相关联。