Search code examples
codeignitersumgroup

CodeIgniter Group with SUM


I want to learn CodeIgniter, with an example case like this:

I have a database (tbl_points)

And I have an output like this:

id sku point
1 001 10
2 001 -1
3 002 5
4 002 -2
5 001 -1

I'm having a hard time making groups based on SKUs and then adding up each value at that point. Can you help me to make it like this? Thank you very much.

id sku point
1 001 8
2 002 3

Solution

  • If you want to write query for ci3.

    $this->db->select('sku');
    $this->db->select_sum('point');
    $this->db->from('tbl_points');
    $this->db->group_by('sku');
    $query = $this->db->get(); 
    $row = $query->result();
              
    print_r($row);