2016-01-25 74 views
2

我有一個查詢model.But第二or_where不工作or_where不笨模型工作

模式

$this->db->select('st_student.st_id'); 
    $this->db->from('st_student'); 
    $this->db->where('st_status',1); 
    $this->db->or_where('st_status',2); 
    if(($from!='') && ($to!='')){ 
     $this->db->where("ab_date BETWEEN '$from' AND '$to'"); 
     $this->db->or_where("as_date BETWEEN '$from' AND '$to'"); 
    } 
    $this->db->group_by('st_student.st_id');  
    $result=$this->db->get(); 

SQL查詢

SELECT `st_student`.`st_id` 
FROM (`st_student`) 
WHERE `st_status` = 1 
OR `st_status` = 2 
AND `ab_date` BETWEEN '01/15/2016' AND '01/26/2016' 
AND `as_date` BETWEEN '01/15/2016' AND '01/26/2016' 
GROUP BY `st_student`.`st_id` 

怎麼啦在那

+0

與實際編輯OP的原帖的問題是,我們不知道該錯誤是否是從該小語法錯誤來還是不來 - 也許是OP要評論? – RamRaider

+0

@Rose你是否得到語法錯誤或者你沒有得到預期的結果? – AnkiiG

+0

我沒有得到預期的結果..運行我的查詢在mysql.i把where語句之間paranthesis ..那個時候得到正確的結果 – robins

回答

3

您沒有得到預期的結果,因爲該查詢沒有條件的括號。

嘗試如下:

$status_condition = 'st_status = 1 or st_status = 2'; 
$this->db->select('st_student.st_id'); 
$this->db->from('st_student'); 
$this->db->where($status_condition); 
if(($from!='') && ($to!='')) 
{ 
    $date_condition = "((ab_date BETWEEN '".$from."' AND '".$to."') or (as_date BETWEEN '".$from."' AND '".$to."'))"; 
    $this->db->where($date_condition); 
} 
$this->db->group_by('st_student.st_id'); 
$result=$this->db->get(); 
0

使用

$this->db->where("st_status = 1 or st_status = 2") 

代替

$this->db->where('st_status',1); 

$this->db->or_where('st_status',2); 
+0

這不是答案 –

+0

是的兄弟..第一個錯誤,但這可能是工作 – Asmal

1

試試這個:

$this->db->select('st_student.st_id'); 
$this->db->from('st_student'); 
$this->db->where('st_status',1, FALSE); 
$this->db->or_where('st_status',2, NULL, FALSE); 
if(($from!='') && ($to!='')){ 
    $this->db->where("ab_date BETWEEN '$from' AND '$to'", FALSE); 
    $this->db->or_where("as_date BETWEEN '$from' AND '$to'", NULL, FALSE); 
} 
$this->db->group_by('st_student.st_id'); 
$result=$this->db->get(); 
2

使用or_where_in

$this->db->select('st_student.st_id'); 
$this->db->from('st_student'); 
$this->db->where('st_status',1); 
$this->db->or_where('st_status',2); 
if((!empty($from)) && (!empty($to))) 
{ 
    $this->db->where('ab_date BETWEEN date("$from", "Y/m/d H:i:s") AND date("$to", "Y/m/d H:i:s") '); 
    $this->db->or_where_in('as_date BETWEEN date("$from", "Y/m/d H:i:s") AND date("$to", "Y/m/d H:i:s") '); 
} 
$this->db->group_by('st_student.st_id');  
$query = $this->db->get(); 
$result = $query->result_array();