从表中选择where子句是interval

时间:2013-03-05 09:08:53

标签: php mysql codeigniter

在此:

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

我想要select count(id),其中order_status_history.new_status是一个区间,例如(2< = order_status_history.new_status< = 6),没有一个单一的int。怎么在这个sql中做这个?

2 个答案:

答案 0 :(得分: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');

这有效吗?

答案 1 :(得分: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);