$this->db->select('sum(commission_amount) as myamount');
$this->db->from('forex_commissions');
$this->db->where('createdDate_sql = `2020-02-16` and userRef = `MXkIP8z0vs5J`');
$result = $this->db->get();
如果我将查询作为原始 SQL 运行,它可以工作,但是当我尝试将其转录为 CodeIgniter 的 ActiveRecord 方法调用时,它会返回
false
。
您没有正确使用
->db->where()
。您的示例的正确 codeigniter 方式是:
$this->db->select('sum(commission_amount) as myamount');
$this->db->from('forex_commissions');
$this->db->where('createdDate_sql', '2020-02-16');
$this->db->where('userRef', 'MXkIP8z0vs5J');
$result = $this->db->get();
使用此语法还会自动转义所有值,从而生成更安全的查询。参见这里
注意:您还可以使用以下命令回显最新执行的查询:
echo $this->db->last_query();