[英]WHERE CLAUSE is not filtering results
I have this working function in my model where I'm trying to get the position of students in a class based on their subject scores.我在我的模型中有这个工作功能,我试图根据学生的学科成绩来获得学生在班级中的位置。
This is working but the where clause does not filter results to classify them based on subjects.这是有效的,但 where 子句不会过滤结果以根据主题对它们进行分类。
model:模型:
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();
}
controller:控制器:
public function GetSubjectPosScores($exam_group_class_batch_exam_subject_id)
{
$data = $this->examresult_model->GetSubjectPosScores($exam_group_class_batch_exam_subject_id);
return $data;
}
view:看法:
<?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);?>
This is what I get with this query:这就是我通过这个查询得到的:
student_id | subject_id | get_tot_score | Position
---------------------------------------------------
11 | 1 | 76 | 3rd
12 | 1 | 90 | 1st
28 | 6 | 89 | 2nd
30 | 6 | 70 | 4th
what I want is this :我想要的是这个:
student_id | subject_id | get_tot_score | Position
---------------------------------------------------
11 | 1 | 76 | 2nd
12 | 1 | 90 | 1st
28 | 6 | 89 | 1st
30 | 6 | 70 | 2nd
the problem is in your query问题出在您的查询中
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
basically your are checking that the value in the column exam_group_class_batch_exam_subject_id
is equal to the value of the column exam_group_class_batch_exam_subject_id
基本上您正在检查
exam_group_class_batch_exam_subject_id
exam_group_class_batch_exam_subject_id
的值
Which is always true这总是正确的
you have to change it passing your value $exam_group_class_batch_exam_subject_id
using some sort of statement (I don't know what library you are using)您必须使用某种语句通过您的值
$exam_group_class_batch_exam_subject_id
更改它(我不知道您使用的是什么库)
edit I take a look at codignirer documentation编辑我看一下 codignirer 文档
just try this试试这个
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();
}
You have specified string value in the where condition and not the value of the variable passed as parameter in the getSubjectPosScores function您在 where 条件中指定了字符串值,而不是在 getSubjectPosScores 函数中作为参数传递的变量的值
Use String Interpolation in your query在查询中使用 字符串插值
Replace代替
public function GetSubjectPosScores($exam_group_class_batch_exam_subject_id) { $sql = "SELECT
get_tot_scoreFROM
exam_group_exam_resultsWHERE
exam_group_class_batch_exam_subject_id=
exam_group_class_batch_exam_subject_idORDER BY
get_tot_scoreDESC"; $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_idORDER BY
get_tot_scoreDESC"; $query = $this->db->query($sql); return $query->result(); }
DESC"; $query = $this->db->query($sql); return $query->result(); }
with this有了这个
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(); }
Or或者
public function GetSubjectPosScores($exam_group_class_batch_exam_subject_id) { $sql = "SELECT
get_tot_scoreFROM
exam_group_exam_resultsWHERE
exam_group_class_batch_exam_subject_id= '".$exam_group_class_batch_exam_subject_id."' ORDER BY
get_tot_scoreDESC"; $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.