2016-03-19 26 views
0

我的表字段是多列选择查询

payid payamount paytype 
01  5000  1 
02  3000  1 
03  2500  3 

我想要得到的结果作为select cash =(select sum (payamount)where paytype=1) online=(select sum (payamount)where paytype=2) check=(select sum (payamount)where paytype=3)

我如何在笨办呢?

+0

payid,payamount,paytype值三行如下。 01,5000,1. 02 3000,1,03 2500,3 – selimhossain

+0

以一种好方法探索你的问题......! – NomanJaved

回答

0

可能是你正在寻找下

$this->db->select("SUM(payment)") 
     ->from("TABLE_NAME") 
     ->where("paytype", $paytype) 

把TABLE_NAME并通过$ paytype,并希望它会工作...

0

听起来像一个GROUP BY。

SELECT sum(paymount), paytype FROM mytable GROUP BY paytype; 

CI中你可以使用

$this->db->select_sum('paymount'); 
$this->db->select('paytype'); 
$this->db->group_by('paytype'); 
$query = $this->db->get('mytable'); 
0
public function getincomebyyear($s,$e){ 
     $this->db->select('income_id, income_date,income_source,income_sourceid,income_refferanceid,income_description,income_amount,(select sum(income_amount) from tbl_incomes where income_description=1) as cash,' 
       . '(select sum(income_amount) from tbl_incomes where income_description=2) as online,' 
       . '(select sum(income_amount) from tbl_incomes where income_description=3) as che'); 
    $this->db->from('tbl_incomes'); 
     $this->db->where('income_date between' 
       . ' "'. date('Y-m-d', strtotime($s)). '" and' 
       . ' "'. date('Y-m-d', strtotime($e)).'"'); 
     $query_result= $this->db->get(); 
     $result=$query_result->result(); 
     return $result; 
    }