如何在codeigniter查询中使用FIND_IN_SET?

时间:2017-10-01 06:08:06

标签: php mysql codeigniter

$array = array('classesID' => 6);
$this->db->select()->from($this->_table_name)->where($array)->order_by($this->_order_by);
$query = $this->db->get();
return $query->result();

我需要获取classesID为6的行。有些行包含6,5,4。   所以我需要使用FIND_IN_SET查询来检索classesID为6的位置。

请指导我

由于

4 个答案:

答案 0 :(得分:1)

'Action history table with studentsIds as comma separated values

I need to get the rows which has student id '4488' and message id '1418'

$id = 1418;
$student_id = 4488;
    this->db->select('id, action_status, updated_on, student_ids');
    $this->db->where('message_id', $id);
    $this->db->where('find_in_set("'.$student_id.'", student_ids) <> 0');
    $this->db->from('actions_history');
    $query = $this->db->get();

It will return a query like

SELECT id, action_status, updated_on, student_ids FROM actions_history  WHERE message_id = '1418' AND find_in_set("4488", student_ids) <>0

答案 1 :(得分:0)

您可以像这样编写查询。

return $this->db->where('classesID' , '6')->order_by('column_name','desc')->get('table_name')->result_array();

如果需要任何帮助评论

答案 2 :(得分:0)

试试这个

$array = array('classesID' => '5,6,7');
$this->db->select();
$this->db->from($this->_table_name);
$this->db->where("FIND_IN_SET('classesID',".$array['classesID'].")",null,false);
$this->db->order_by($this->_order_by);
$query = $this->db->get();
return $query->result();

答案 3 :(得分:0)

对于那些正在寻找基于模型 (CI v4.x) 的解决方案的人,

$modal->where("FIND_IN_SET('<string to find>', '<column name>')", null, false)->findAll();

就你而言,

$modal->where("FIND_IN_SET('".$array['classesID']."', 'classesID')", null, false)->findAll();