Search code examples
phpmysqlcodeigniterquery-builderwhere-in

How to implement row constructor syntax using CodeIgniter's where_in() query building method


Below is CodeIgniter single column where_in clause $this->db->where_in('x1',$val);

how can I pass multiple column in CodeIgniter where_in clause like below MySQL query:

select *
from tab1
where (col1,col2) in ((1,2),(2,3))`

Solution

  • Assume your data array is like this(should be)

    $val1 = array(1,2);
    $val2 = array(2,3);
    

    And query should be

    $this->db->select('*');
    $this->db->from('tab1');
    $this->db->where_in('col1',$val1);
    $this->db->or_where_in('col2',$val2);
    $query = $this->db->get();
    $result = $query->result_array();
    

    Or else you can use

    $this->db->query("select * from tab1 where (col1,col2) in ($val1,$val2)");