1

我在这里有一个应用程序:应用程序

该应用程序在那里向您显示我想用作过滤器的两个下拉菜单,以便能够获得学生的答案来确定所选学生和从相应下拉菜单中选择的问题。

要查看下拉菜单,请在应用程序中AssessmentAssessment下拉菜单中选择一个并提交,您将看到下方显示的学生和问题下拉菜单。

现在我要做的是根据用户单击Get Student Answers按钮时从学生和问题下拉菜单中选择的选项创建一个动态 WHERE 子句。

以下是当前查询。SessionId = ?查询必须始终在 WHERE 子句中具有默认子句。其他子句取决于用户从两个下拉菜单中选择的选项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>

我在想,如果选择了特定的学生,则将其包含StudentId = ?在 WHERE 子句中,如果选择了特定的问题编号,则将其包含QuestionId = ?在 WHERE 子句中。但是,如果All在学生下拉菜单中选择了该值,StudentId = ?则从 WHERE 子句中删除,因为我们正在查找所有学生,而不是针对特定学生进行缩减。如果All从问题下拉菜单中选择值但显然处理QuestionId = ?

4

2 回答 2

0

你知道你可以在这里写一个子查询吗?

WHERE (SessionId = ?)

喜欢

WHERE SessionId IN(SELECT SessionId FROM ...)

http://dev.mysql.com/tech-resources/articles/subqueries_part_1.html

于 2013-01-27T02:32:33.323 回答
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
    ";
    }
于 2013-01-27T03:44:17.207 回答