簡體   English   中英

WHERE CLAUSE 不過濾結果

[英]WHERE CLAUSE is not filtering results

我在我的模型中有這個工作功能,我試圖根據學生的學科成績來獲得學生在班級中的位置。

這是有效的,但 where 子句不會過濾結果以根據主題對它們進行分類。

模型:

public function GetSubjectPosScores($exam_group_class_batch_exam_subject_id) 
    {
        $sql = "SELECT `get_tot_score` FROM `exam_group_exam_results` 
WHERE `exam_group_class_batch_exam_subject_id`= `exam_group_class_batch_exam_subject_id` ORDER BY `get_tot_score` DESC";
        $query = $this->db->query($sql);
        return $query->result();
    }

控制器:

public function GetSubjectPosScores($exam_group_class_batch_exam_subject_id)
    {
        $data = $this->examresult_model->GetSubjectPosScores($exam_group_class_batch_exam_subject_id); 
        return $data;
    }

看法:

<?php
$scores2 = $CI->GetSubjectPosScores($exam_group_class_batch_exam_subject_id); //echo $value->pos;
$scores = array_column($scores2, 'get_tot_score');
$pos = array_search($exam_result_value->get_tot_score, $scores);
$number = $pos + 1;
 echo $CI->ordinal($number);?>

這就是我通過這個查詢得到的:

student_id | subject_id | get_tot_score | Position
---------------------------------------------------
 11        |   1        |   76          |  3rd
 12        |   1        |   90          |  1st
 28        |   6        |   89          |  2nd
 30        |   6        |   70          |  4th

我想要的是這個:

student_id | subject_id | get_tot_score | Position
---------------------------------------------------
 11        |   1        |   76          |  2nd
 12        |   1        |   90          |  1st
 28        |   6        |   89          |  1st
 30        |   6        |   70          |  2nd

問題出在您的查詢中

SELECT `get_tot_score` FROM `exam_group_exam_results` 
WHERE `exam_group_class_batch_exam_subject_id`= `exam_group_class_batch_exam_subject_id` ORDER BY `get_tot_score` DESC

基本上您正在檢查exam_group_class_batch_exam_subject_id exam_group_class_batch_exam_subject_id的值

這總是正確的

您必須使用某種語句通過您的值$exam_group_class_batch_exam_subject_id更改它(我不知道您使用的是什么庫)

編輯我看一下 codignirer 文檔

試試這個

public function GetSubjectPosScores($exam_group_class_batch_exam_subject_id) 
    {
        $sql = "SELECT `get_tot_score` FROM `exam_group_exam_results` 
WHERE `exam_group_class_batch_exam_subject_id`= ? ORDER BY `get_tot_score` DESC";
        $query = $this->db->query($sql, [$exam_group_class_batch_exam_subject_id]);
        return $query->result();
    }

您在 where 條件中指定了字符串值,而不是在 getSubjectPosScores 函數中作為參數傳遞的變量的值

在查詢中使用 字符串插值

代替

public function GetSubjectPosScores($exam_group_class_batch_exam_subject_id) { $sql = "SELECT FROM ,其中WHERE = exam_group_class_batch_exam_subject_id ORDER BY get_tot_score DESC"; $query = $this->db->query($sql); return $query->result(); } DESC"; $query = $this->db->query($sql); return $query->result(); }

有了這個

public function GetSubjectPosScores($exam_group_class_batch_exam_subject_id) { $sql = "SELECT get_tot_score FROM exam_group_exam_results WHERE exam_group_class_batch_exam_subject_id= $exam_group_class_batch_exam_subject_id ORDER BY get_tot_score DESC"; $query = $this->db->query($sql); return $query->result(); }

或者

public function GetSubjectPosScores($exam_group_class_batch_exam_subject_id) { $sql = "SELECT FROM ,其中WHERE = '".$exam_group_class_batch_exam_subject_id."' ORDER BY get_tot_score DESC"; $query = $this->db->query($sql); return $query->result(); } DESC"; $query = $this->db->query($sql); return $query->result(); }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM