Search code examples
mysqlcodeigniterwhere-in

Mysql WHERE IN not working when i put swap the comma seperated value


I have this below weird problem with my mysql query. Correct me if i am wrong.

This below query( Which i have printed from Codeigniter ) giving me result with subject = 4.

WHERE `notes`.`subject` IN ('4,2')  GROUP BY `notes`.`id` ORDER BY `created_date` DESC

But when i changed this

WHERE `notes`.`subject` IN ('2,4')  GROUP BY `notes`.`id` ORDER BY `created_date` DESC

It is not returning any result. Why is that? Lets say i have only one result in table and i am using codeigniter for this.

$this->db->where_in('notes.subject',$this->input->get('subject'));

Solution

  • the issue is here

    $this->db->where_in('notes.subject',$this->input->get('subject'));
    

    the input variable subject is a string for eg '2,4'

    you can use $this->db->where_in('notes.subject',explode(",",$this->input->get('subject')));

    which will pass an array to where_in

    and as per the User guide

    Second parameter to where_in() is an array