2014-02-06 92 views
0

我想在笨格式寫這篇以下查詢,並得到一些問題,聚合函數:在笨查詢

$stmt = "select sum(subscription_amt) as samt, bill_month, 
       sum(loan_refund_amt*no_of_loan_installment+error_amt) as lamt 
     from pf_bill_det 
     where trim(pf_number)='$pfno' 
       and fin_year='$fyear' 
       and aproved='Y' group by bill_month"; 
$query = $this->db->query($stmt); 

這個查詢有錯誤loan_refund_amt*no_of_loan_installment+error_amt is not a column結束了。請幫助我如何使用codeigniter查詢格式編寫此查詢。

+0

嘗試'$ this-> db-> count();'counter函數在codeigniter中... – user2727841

+0

請確保您的表中有這些列'loan_refund_amt * no_of_loan_installment + error_amt'或嘗試'loan_refund_amt * no_of_loan_installment + error_amt' –

+0

檢查您在查詢中使用的表格列或在此處顯示錶格列 – NLSaini

回答

0

試試這個,

$query=$this->db->query("select sum(subscription_amt) as samt,bill_month,sum(`loan_refund_amt`*`no_of_loan_installment`+`error_amt`) as lamt from pf_bill_det where trim(pf_number)='$pfno' and fin_year='$fyear' and aproved='Y' group by bill_month"); 
2

你爲什麼不試試這個

$this->db->select("COUNT(*) AS MyCount"); 
$this->db->from("MyTable"); 
$this->db->where("field", $value); 
$this->db->get(); 

OR

$this->db->select("SUM(field_name) AS MySum"); 
$this->db->from("MyTable"); 
$this->db->where("field", $value); 
$this->db->get(); 

OR

$this->db->select("SUM(field_name) AS MySum, username, password"); 
$this->db->from("MyTable"); 
$this->db->where("field", $value); 
$this->db->get(); 

在簡單的查詢功能,您可以使用

$query = $this->db->query("SELECT COUNT(field_name) AS total_names, fname");

$query->result(); \\ Returns an array of objects

$query->result_array(); \\ Returns result as a pure array

$query->row(); \\ Returns a single result and first row