Codeigniter select query with AND and OR condition

2019-01-12 09:27发布

问题:

I want a Codeigniter select query from a table with three conditions in .

1. wrk_fld_exc = 140
2. wrk_cs_sts = Open
3. wrk_dlvrd_sts = Delivered OR wrk_cl_sts = Success

The third condition is an AND condition contains OR condition. First and second is And condition.

回答1:

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);


回答2:

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



回答3:

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');


回答4:

$this->db->where ('attribute',$data['attribute']);
$this->db->or_where ('attribute',$data['attribute']);
$this->db->and_where ('attribute',$data['attribute']);


回答5:

 $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);


回答6:

    $this->db->select("*");
    $this->db->from("table_name");
    if($condition1 != ''){
        $this->db->where('wrk_fld_exc', 140);
    }
    if($condition2 != ''){
        $this->db->where('wrk_cs_sts ', open);
    }
    //You can limit the results
    $this->db->limit(5);
    $q = $this->db->get();
    return $q->result();

This is the basic structure of the query you can implement in this way in codeigniter. You can add conditions if you require.



标签: codeigniter