简体   繁体   English

如何创建mysqli动态WHERE子句

[英]How to create a mysqli dynamic WHERE clause

I have an application here: Application 我这里有一个应用程序: 应用程序

The application is there to show you the two drop down menus I want to use as a filter to be able to get Student's Answers determining on the student(s) selected and the question(s) selected form the respective drop down menus. 该应用程序在那里向您展示了两个我想用作过滤器的下拉菜单,以便能够从相应的下拉菜单中确定所选学生和所选问题的学生答案。

To see the drop down menus, in the application select an Assessment from the Assessment drop down menu and submit, you will see the students and questions drop down menus displayed underneath. 看到下拉菜单,在应用程序中选择一个AssessmentAssessment下拉菜单并提交,你会看到学生和问题下拉下方显示的菜单。

Now what I want to do is to create a dynamic WHERE clause depending on the options selected from the student(s) and question(s) drop down menus when the user clicked on the Get Student Answers button. 现在,我想做的是根据用户单击“ Get Student Answers按钮时从学生和问题下拉菜单中选择的选项来创建动态WHERE子句。

Below is the current query. 以下是当前查询。 The query has to have the default clause SessionId = ? 查询必须具有默认子句SessionId = ? at all times in the WHERE clause. 始终在WHERE子句中。 The other clauses studentId = ? 其他子句studentId = ? and questionId = ? questionId = ? depends on the options the user has chose from both drop downs. 取决于用户从两个下拉菜单中选择的选项。

$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(); 

Below is the Students and Questions drop down menus as a sample html: 以下是学生和问题下拉菜单,作为示例html:

Students Drop Down Menu: 学生下拉菜单:

<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>

Questions Drop Down Menu: 问题下拉菜单:

<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>

I am thinking of something like if a specific student is selected then include StudentId = ? 我在想什么,如果选择了一个特定的学生然后包括StudentId = ? in WHERE clause, if specific question number is selected then include QuestionId = ? 在WHERE子句中,如果选择了特定的问题编号,则包括QuestionId = ? in WHERE clause. 在WHERE子句中。 But if the All value is selected in Student drop down menu then remove StudentId = ? 但是,如果在“学生”下拉菜单中选择了“ All值,则删除StudentId = ? from the WHERE clause as we are look for all student, not whittling down for a specific student. 从WHERE子句中查找,因为我们正在寻找所有学生,而不是为特定学生而苦恼。 This is same for if All value is selected from Question drop down menu but obviously dealing with QuestionId = ? 如果从“问题”下拉菜单中选择“ All值”,但显然处理QuestionId = ?则这是相同的QuestionId = ?

Did you know that you can write a subquery here? 您知道您可以在此处编写子查询吗?

WHERE (SessionId = ?)

like 喜欢

WHERE SessionId IN(SELECT SessionId FROM ...)

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

Ok, you know what? 好吧,你知道吗? I think I have misunderstood the real question here. 我想我在这里误解了真正的问题。 You may find this to work better: 您可能会发现它可以更好地工作:

    $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
    ";
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM