Codeigniter $ this-> db-> select()不适用于Round(Max())或FORMAT(MAX())

时间:2014-05-23 06:46:16

标签: php mysql codeigniter

我写下面的代码

$this->db->select('SUM(qty) as total_qty,(FORMAT(SUM(amount),2)) as total_amount');
$this->db->where('Invoice_Rec_No',$Invoice_Rec_No);
$result=$this->db->get($this->invoice_products_tbl);
$total_data=$result->row(); 

但是收到错误

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM (tbl_invoice_products) WHERE Invoice_Rec_No = 7' at line 2

SELECT SUM(qty) as total_qty, (FORMAT(SUM(amount), 2)) as total_amount FROM (tbl_invoice_products) WHERE Invoice_Rec_No = 7

Filename: C:\wamp\www\admin_followme247_master\system\database\DB_driver.php

我想用codeigniter ActiveRecord执行此查询。

2 个答案:

答案 0 :(得分:3)

db FALSE方法

中的用户第二个参数select
    $this->db->select('SUM(qty) as total_qty,
                       (FORMAT(SUM(amount),2)) as total_amount', FALSE);

CI db类是自动添加(apostrophe) when manipulate sql query, if you pass send parameter FALSE in select`方法然后它与输入保持一致。

答案 1 :(得分:0)

您可以像这样格式化代码

$select = array(
    'SUM(qty) as total_qty',
    '(FORMAT(SUM(amount),2)) as total_amount'
);
$this->db->select($select);
$this->db->where('Invoice_Rec_No',$Invoice_Rec_No);
$result=$this->db->get($this->invoice_products_tbl);
$total_data=$result->row(); 

对于简单的列选择,可以使用字符串,但对于像这样的复杂选择,您可以使用数组或单独选择每个列