Get the result of SUM and group field from Active record in Codeigniter

前端 未结 2 953
执念已碎
执念已碎 2021-01-16 23:21

I need to build this query in Codeigniter but I don\'t know how to get the result of the SUM:

SELECT 
    description, SUM(amount)
FROM
    PAYMENT
WHERE
            


        
相关标签:
2条回答
  • 2021-01-17 00:10

    Setting FALSE as second parameter, 'select' allows to write a custom sentence.

    $this->db->select('description, SUM(amount) AS amount', FALSE);
    $this->db->where('date_payment >=', $min_date);
    $this->db->where('date_payment <=', $max_date);
    $this->db->group_by("description");
    $qwer = $this->db->get('PAYMENT');
    

    Documentation for CI2
    Documentation for CI3

    0 讨论(0)
  • 2021-01-17 00:17

    try something like this:

    $query = $this->db->select_sum('amount', 'Amount');
    $query = $this->db->where(...)
    $query = $this->db->get('payment');
    $result = $query->result();
    
    return $result[0]->Amount;
    
    0 讨论(0)
提交回复
热议问题