2013-03-05 78 views
0

在此:選擇where子句是間隔

$this->db->select('count(o.id)'); 
    $this->db->from('orders o');   
      $this->db->join('order_status_history', 'o.id = order_status_history.order_id'); 
      $this->db->where("(order_status_history.in_dts)::date ='" .$reportDay. "'"); 
      $this->db->where('order_status_history.new_status ' , 6); 

我想選擇計數(ID),其中order_status_history.new_status是一個間隔例如(2 < = order_status_history.new_status < = 6),沒有一個單一INT。如何在這個SQL中做到這一點?

回答

2

正如我記得你可以添加Where語句作爲一個字符串,就像在一個本地的SQL查詢。 請試試這個:

$this->db->select('count(o.id)'); 
    $this->db->from('orders o');   
     $this->db->join('order_status_history', 'o.id = order_status_history.order_id'); 
     $this->db->where("(order_status_history.in_dts)::date ='" .$reportDay. "'"); 
     $this->db->where('order_status_history.new_status BETWEEN 2 AND 6'); 

這工作正常?

+0

工作的感謝大家! – 2013-03-05 09:24:25

2

你覺得這是什麼:

$this->db->select('count(o.id)'); 
$this->db->from('orders as o');   
$this->db->join('order_status_history', 'o.id = order_status_history.order_id'); 
$this->db->where("(order_status_history.in_dts)::date ='" .$reportDay. "'"); 
$this->db->where_between('order_status_history.new_status ',2 , 6); 
+0

使用where_between來引入id間隔! – goseo 2013-03-05 09:17:26