[英]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_idORDER BY
get_tot_scoreDESC"; $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_scoreDESC"; $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.