2012-03-30 32 views
10

Tôi muốn truy vấn chọn Codeigniter từ bảng có ba điều kiện.Codeigniter chọn truy vấn có điều kiện AND và OR

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

Điều kiện thứ ba là điều kiện VÀ điều kiện HOẶC. Đầu tiên và thứ hai là Và điều kiện.

Trả lời

21

Bạn có thể mã hóa nó như thế này:

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

Như thế này

$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'); 
0
$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); 
0
$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(); 

Đây là cấu trúc cơ bản của truy vấn mà bạn có thể thực hiện theo cách này trong CodeIgniter. Bạn có thể thêm điều kiện nếu bạn yêu cầu.

4

CodeIgniter sử dụng cú pháp riêng của mình cho HOẶC claus trong truy vấn

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

để sử dụng và trong nơi sử dụng khoản $this->db->where(''); hai lần

Các vấn đề liên quan