Nirmal kumar
Nirmal kumar

Reputation: 65

How to use FIND_IN_SET in codeigniter query?

$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();

I need to get the rows where the classesID is 6. Some rows contain 6,5,4. so i need to use FIND_IN_SET query to retrive where the classesID is 6.

Please guide me

Thanks

Upvotes: 4

Views: 16926

Answers (4)

Divyang Dodiya
Divyang Dodiya

Reputation: 81

For those who're looking for model (CI v4.x) based solution,

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

In your case,

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

Upvotes: 0

'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

Upvotes: 6

Kundan Prasad
Kundan Prasad

Reputation: 576

Try this

$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();

Upvotes: 1

Gaurav
Gaurav

Reputation: 442

You can write the query like this.

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

if Need any help comment

Upvotes: 0

Related Questions