Codeigniter select query with AND and OR condition

I want to add some info for Codeigniter 3.1.11:

There is a Query grouping in Query Builder Class

So Ben Answer will be like this:

public function some_function()
    {
        $this->db->where("wrk_fld_exc",140);
        $this->db->where("wrk_cs_sts","Open");
        $this->db->group_start();
        $this->db->where("wrk_dlvrd_sts","Delivered");
        $this->db->or_where("wrk_cl_sts","Success");
        $this->db->group_end();
        return $this->db->get("some_table");
    }

After check with:

        echo $this->db->last_query();

It will produce SQL:

SELECT * FROM `some_table` WHERE `wrk_fld_exc` = 140 AND `wrk_cs_sts` = 'Open' AND ( `wrk_dlvrd_sts` = 'Delivered' OR `wrk_cl_sts` = 'Success' )

Further Read: Codeigniter Manual Book


codeigniter uses its own syntax for OR claus in query

$this->db->or_where('wrk_cl_sts','Success'); 

to use AND in where clause use $this->db->where(''); twice


You can code it like this:

       $this->db->where('wrk_fld_exc',140);
       $this->db->where('wrk_cs_sts','open');
       $where = '(wrk_dlvrd_sts="open" or wrk_cl_sts = "Success")';
       $this->db->where($where);

Like this

 $this->db->where('wrk_fld_exc',140);
 $this->db->where('wrk_cs_sts','open');
 $this->db->where('wrk_dlvrd_sts ','Delivered');
 $this->db->or_where('wrk_cl_sts','Success');

Tags:

Codeigniter