如何在codeigniter查询中使用FIND_IN_SET?

问题描述 投票:0回答:3
$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的位置。

请指导我

谢谢

php mysql codeigniter
3个回答
1
投票

'Action history table with studentsIds as comma separated values

我需要获取学生ID为“4488”且消息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();

它将返回一个类似的查询

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

0
投票

您可以像这样编写查询。

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

如果需要任何帮助评论


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();
© www.soinside.com 2019 - 2024. All rights reserved.