如何在codeigniter中的select语句中使用between子句

时间:2015-03-26 08:37:49

标签: php sql codeigniter

我试图通过从一个表中获取一个数据行并将这些值用作where条件来从数据库获取数据。然后我尝试将json字符串传递给控制器​​类。但是我无法达到这个目的。我应该怎么写这段代码 这是我用来获取数据的代码。

 $query = $this->db->query("SELECT * FROM todaywork WHERE idEmployee = $user ");

        if ($query->num_rows() > 0)
        {
           $row = $query->row(); 

               $from = $row->T_frange;
               $to = $row->T_trange;
               $note = $row->T_note;
        }     
               $this->db->select("cus.idCustomer,cus.C_name,f.F_itemname,ctr.*");
                //$whereCondition = array('cus.idCustomer' => $_POST['custId']);
                $this->db->where('C_trnoofarr BETWEEN {$from} AND {$to}');
                //filter where
                $this->db->from('customer cus');
                $this->db->join('contracts AS ctr', 'cus.idCustomer = ctr.idCustomer', 'INNER');
                $this->db->join('facility AS f','ctr.idContracts = f.idContracts','INNER');
                $query = $this->db->get();

                return $query->result();

    }

3 个答案:

答案 0 :(得分:1)

替换它,

$this->db->where('C_trnoofarr BETWEEN {$from} AND {$to}');

有了这个,

$this->db->where('C_trnoofarr >', $from);
$this->db->where('C_trnoofarr <', $to);

如果你想在WHERE条款中使用raw,你也可以添加第三个参数。

$this->db->where('<where clause>', NULL, FALSE);

来自docs

  

$ this-&gt; db-&gt; where()接受可选的第三个参数。如果将其设置为FALSE,CodeIgniter将不会尝试使用反引号来保护您的字段或表名。

在你的情况下,也许是这样,

$this->db->where('C_trnoofarr BETWEEN {$from} AND {$to}', NULL, FALSE);

答案 1 :(得分:1)

之间使用此功能
$this->db->where('C_trnoofarr >', $from);
$this->db->where('C_trnoofarr <', $to);

答案 2 :(得分:-1)

                $this->db->select('*'); 
                $this->db->from('company');
                $this->db->where("grade BETWEEN $first AND $second");
                $this->db->get()->result();