robins
robins

Reputation: 1668

Check the conditions in comma separated values

I'm using CodeIgniter. In my table one of the field contain value like 1,2 or 1 or 2. Now I want to check a condition in model. The column name is period that value is mentioned above.

model

  if($post_period == 1)
        {
            $this->db->where('period',1);             
        }
        else if($post_period == 2)  
            {
                $this->db->where('period',2);      
            }

Upvotes: 0

Views: 1813

Answers (1)

Praveen Kumar
Praveen Kumar

Reputation: 2408

Use Mysql Function FIND_IN_SET(str,strlist) .

Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings.

A string list is a string composed of substrings separated by “,” characters. If the first argument is a constant string and the second is a column of type SET, the FIND_IN_SET() function is optimized to use bit arithmetic.

Returns 0 if str is not in strlist or if strlist is the empty string. Returns NULL if either argument is NULL.

This function does not work properly if the first argument contains a comma (“,”) character.

 $this->db->where("FIND_IN_SET( '$post_period' , period) ");   

Upvotes: 4

Related Questions