Dingo Ghatr
Dingo Ghatr

Reputation: 116

Codeigniter Mysql column with comma seperated values

I wnat to test a field with multiple values against single value, my SQL is like .

WHERE id (1);

Database values are like

id
---
1,2

2,3

1,3,4

codeigniter

$this->db->select('*');
$this->db->from('content');
$this->db->where('name', $name);
$this->db->where('id', $id);

I tried

$this->db->where_in('id', explode(',', $id));

but its not working.Please help me to solve this.

Upvotes: 3

Views: 2795

Answers (3)

Gufran Hasan
Gufran Hasan

Reputation: 9373

Please try this one:

$this->db->select('*')->from('table_name')
       ->where("column_name LIKE '%$key_values%'")->get();

Upvotes: 0

M Khalid Junaid
M Khalid Junaid

Reputation: 64476

To find the values from a given set you can use FIND_IN_SET

$this->db->where("FIND_IN_SET(".$id.",id) >", 0);

Note: This type of design is considered as a bad design and you should normalize your structure, I suggest you to have a look at Database Normalization

Upvotes: 6

Abdulla Nilam
Abdulla Nilam

Reputation: 38609

What if you use like() instead of where()

$this->db->like('id', $id);
// Produces: WHERE `id` LIKE '%2%' ESCAPE '!'

Note: If 2 is the value 2,22,222 will pass through this

Upvotes: 2

Related Questions