3

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.

4
  • what is the issue with where_in? Commented Mar 14, 2018 at 5:47
  • where_in is working if the id value(eg.(1,2)) and database value(1,2) are same.If id value is 1 and database value is(1,2) its not working.@ Kaleem Commented Mar 14, 2018 at 5:52
  • 1
    Then like() is not good option for you. It will get records having 2,12,22,32 ...222 and so on when you will search with id=2. Please look at this option. Looks good for your problem Commented Mar 14, 2018 at 6:04
  • Thanks for your suggestion @Kaleem Commented Mar 14, 2018 at 6:11

3 Answers 3

6

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

0

Please try this one:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.