如何在codeigniter中的select语句中使用between子句
问题描述:
I'm trying to get data from database by taking one data row from one table and use those values as where condition. and then i tried pass json string to controller class. but i'm unable to reach this. how should i write this code This is code i used to get data.
$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();
}
我试图通过从一个表中获取一个数据行并使用这些值作为where条件从数据库获取数据 。 然后我尝试将json字符串传递给控制器类。 但是我无法达到这一点。 我该怎么写这段代码 这是我用来获取数据的代码。 p>
$ query = $ this-> db-> query(“SELECT * FROM todaywork WHERE idEmployee = $ user“);
if if($ query-> num_rows()> 0)
{
$ row = $ query-> row();
$ from = $ row-> T_frange;
$ to = $ row-> T_trange;
$ note = $ row-> T_note;
}
$ this-> db- >选择(“cus.idCustomer,cus.C_name,f.F_itemname,ctr。*”);
// $ whereCondition = array('cus.idCustomer'=> $ _POST ['custId']); \ n $ this-> db-> where('C_trnoofarr BETWEEN {$ from} AND {$ to}');
//过滤其中
$ this-> db-> from('customer cus' );
$ this-> db-> join('contract AS ctr','cus.idCustomer = ctr.idCustomer','INNER');
$ this-> db-> join(' facility AS f','ctr.idContracts = f.idContracts','INNER');
$ query = $ this-> db-> get();
返回$ query->结果( );
}
code> pre>
div>
答
Use this for between
$this->db->where('C_trnoofarr >', $from);
$this->db->where('C_trnoofarr <', $to);
答
Replace this,
$this->db->where('C_trnoofarr BETWEEN {$from} AND {$to}');
with this,
$this->db->where('C_trnoofarr >', $from);
$this->db->where('C_trnoofarr <', $to);
Still if you want to use raw in WHERE
clause you've to add a third parameter as well.
$this->db->where('<where clause>', NULL, FALSE);
From docs:
$this->db->where() accepts an optional third parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks.
In your case maybe like this,
$this->db->where('C_trnoofarr BETWEEN {$from} AND {$to}', NULL, FALSE);