codeigniter WHERE CLAUSE未筛选结果

6ojccjat  于 2022-12-07  发布在  其他
关注(0)|答案(2)|浏览(113)

我在我的模型中有一个工作函数,我试图根据学生的学科成绩来得到他们在班级中的位置。
这是有效的,但是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
uqzxnwby

uqzxnwby1#

问题出在您查询中

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(我不知道您使用的是什么库)

edit我看一下codignier文档

试试这个

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();
    }
laximzn5

laximzn52#

您在where条件中指定了字符串值,而不是在getSubjectPosScores函数中作为参数传递的变量值
在查询中使用String Interpolation
替换
public function GetSubjectPosScores($exam_group_class_batch_exam_subject_id) { $sql = "SELECT获取总分FROM考试组考试结果WHERE考试组班级批次考试科目标识=考试组班级批次考试科目标识ORDER BY获取总分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获取总分DESC"; $query = $this->db->query($sql); return $query->result(); }

相关问题