我在这里有一个申请:Application
该应用程序将向您显示我想用作过滤器的两个下拉菜单,以便能够获得学生答案确定所选学生以及从相应下拉菜单中选择的问题
要查看下拉菜单,请在应用程序中从Assessment
下拉菜单中选择Assessment
并提交,您将看到下方显示的学生和问题下拉菜单。
现在我要做的是创建一个动态WHERE子句,具体取决于当用户点击Get Student Answers
按钮时从学生和问题下拉菜单中选择的选项。
以下是当前查询。查询必须在WHERE子句中始终具有默认子句SessionId = ?
。其他子句studentId = ?
和questionId = ?
取决于用户的选项
从两个下降中选择了。
$selectedstudentanswerqry = "
SELECT
StudentAlias, q.SessionId, QuestionNo, QuestionContent, o.OptionType, GROUP_CONCAT( DISTINCT Answer
ORDER BY Answer SEPARATOR ',' ) AS Answer, r.ReplyType,
GROUP_CONCAT(DISTINCT StudentAnswer ORDER BY StudentAnswer SEPARATOR ',') AS StudentAnswer, ResponseTime
FROM Student s
INNER JOIN Student_Answer sa ON (s.StudentId = sa.StudentId)
INNER JOIN Student_Response sr ON (sa.StudentId = sr.StudentId)
INNER JOIN Question q ON (sa.QuestionId = q.QuestionId)
INNER JOIN Answer an ON q.QuestionId = an.QuestionId
LEFT JOIN Reply r ON q.ReplyId = r.ReplyId
LEFT JOIN Option_Table o ON q.OptionId = o.OptionId
WHERE (SessionId = ?)
GROUP BY sa.StudentId, q.QuestionId
ORDER BY StudentAlias, q.SessionId, QuestionNo
";
global $mysqli;
$selectedstudentanswerstmt=$mysqli->prepare($selectedstudentanswerqry);
// You only need to call bind_param once
$selectedstudentanswerstmt->bind_param("i",$_POST["session"]);
// get result and assign variables (prefix with db)
$selectedstudentanswerstmt->execute();
$selectedstudentanswerstmt->bind_result($detailsStudentAlias,$detailsSessionId,$detailsQuestionNo,
$detailsQuestonContent,$detailsOptionType,$detailsAnswer,$detailsReplyType,$detailsStudentAnswer,$detailsResponseTime);
$selectedstudentanswerstmt->store_result();
$selectedstudentanswernum = $selectedstudentanswerstmt->num_rows();
下面是学生和问题下拉菜单,作为示例html:
学生下拉菜单:
<select name="student" id="studentsDrop">
<option value="All">All</option>
<option value="3">u0499220 - Jack Briggs</option>
<option value="7">u0093220 - Mary Kay</option>
</form>
问题下拉菜单:
<select name="question" id="questionsDrop">
<option value="All">All</option>
<option value="34">1</option>
<option value="35">2</option>
<option value="36">3</option>
</form>
我正在考虑如果选择了特定学生,然后在WHERE子句中包含StudentId = ?
,如果选择了特定的问题编号,则在WHERE子句中包含QuestionId = ?
。但是,如果在学生下拉菜单中选择All
值,则从WHERE子句中删除StudentId = ?
,因为我们正在寻找所有学生,而不是为特定学生减少。如果从问题下拉菜单中选择All
值,但明显处理QuestionId = ?
答案 0 :(得分:0)
你知道你可以在这里写一个子查询吗?
WHERE (SessionId = ?)
像
WHERE SessionId IN(SELECT SessionId FROM ...)
http://dev.mysql.com/tech-resources/articles/subqueries_part_1.html
答案 1 :(得分:0)
好的,你知道吗?我想我在这里误解了真正的问题。您可能会发现这更好用:
$selectedstudentanswerqry = "
SELECT
StudentAlias, q.SessionId, QuestionNo, QuestionContent, o.OptionType, GROUP_CONCAT( DISTINCT Answer
ORDER BY Answer SEPARATOR ',' ) AS Answer, r.ReplyType,
GROUP_CONCAT(DISTINCT StudentAnswer ORDER BY StudentAnswer SEPARATOR ',') AS StudentAnswer, ResponseTime
FROM Student s
INNER JOIN Student_Answer sa ON (s.StudentId = sa.StudentId)
INNER JOIN Student_Response sr ON (sa.StudentId = sr.StudentId)
INNER JOIN Question q ON (sa.QuestionId = q.QuestionId)
INNER JOIN Answer an ON q.QuestionId = an.QuestionId
LEFT JOIN Reply r ON q.ReplyId = r.ReplyId
LEFT JOIN Option_Table o ON q.OptionId = o.OptionId";
if (studentId = something && questionId = something) {
$selectedstudentanswerqry .= "WHERE (SessionId = ?)
GROUP BY sa.StudentId, q.QuestionId
ORDER BY StudentAlias, q.SessionId, QuestionNo
";
} else if (studentId = somethingelse && questionId = somethingelse) {
$selectedstudentanswerqry .= "WHERE (SessionId = ?)
GROUP BY sa.StudentId, q.QuestionId
ORDER BY StudentAlias, q.SessionId, QuestionNo
";
} else {
$selectedstudentanswerqry .= "WHERE (SessionId = ?)
GROUP BY sa.StudentId, q.QuestionId
ORDER BY StudentAlias, q.SessionId, QuestionNo
";
}