# How to sum values from a table in cakephp3

**URL:** <https://discourse.cakephp.org/t/how-to-sum-values-from-a-table-in-cakephp3/1926>\
**Category:** Need Help\
**Created:** [January 25, 2017, 7:52am UTC](https://discourse.cakephp.org/t/how-to-sum-values-from-a-table-in-cakephp3/1926 "2017-01-25T07:52:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![amaro](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@amaro](https://discourse.cakephp.org/u/amaro)\
**Post date:** [January 25, 2017, 7:52am UTC](https://discourse.cakephp.org/t/how-to-sum-values-from-a-table-in-cakephp3/1926/1 "2017-01-25T07:52:33Z")

</div>

Good afternoon, I would like your help to do a procedure of sum in a MySQL database I have the following scenario:

I have a table with the branch column and another total, precise sum total value of each branch for example:

RN = 10  
PE = 20  
PB = 10  
RN = 20  
PE = 30

I want to add the value of RN and PE

RN = 30  
PE = 50

In mysql I can do it but I’m not sure how to do it in cakephp3 I’m using the following query in mysql (SELECT SUM(total)from comandas where branch = ‘RN’),  
How do I do it in the cake?

---

<div class="post-metadata">

**Author:** ![SATHISH](https://avatars.discourse-cdn.com/v4/letter/s/858c86/32.png) [@SATHISH](https://discourse.cakephp.org/u/SATHISH)\
**Post date:** [January 25, 2017, 7:55am UTC](https://discourse.cakephp.org/t/how-to-sum-values-from-a-table-in-cakephp3/1926/2 "2017-01-25T07:55:33Z")

</div>

You can use [Collections](https://book.cakephp.org/3.0/en/core-libraries/collections.html)

---

<div class="post-metadata">

**Author:** ![amaro](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@amaro](https://discourse.cakephp.org/u/amaro)\
**Post date:** [January 25, 2017, 5:37pm UTC](https://discourse.cakephp.org/t/how-to-sum-values-from-a-table-in-cakephp3/1926/3 "2017-01-25T17:37:54Z")

</div>

> [@SATHISH](#):
>
> You can use

I do not understand how I can use the class collection because in my point of view it has an array list and the data I want to sum is saved in a MySQL database if I can give some example I am grateful

---

<div class="post-metadata">

**Author:** ![risrik](https://avatars.discourse-cdn.com/v4/letter/r/958977/32.png) [@risrik](https://discourse.cakephp.org/u/risrik)\
**Post date:** [January 31, 2017, 4:42pm UTC](https://discourse.cakephp.org/t/how-to-sum-values-from-a-table-in-cakephp3/1926/4 "2017-01-31T16:42:06Z")

</div>

You can get the sum of each branch by using a Collection like this:  
$mapper = function ($comanda, $key, $mapReduce) {  
$mapReduce-\>emitIntermediate($comanda-\>total, $comanda-\>branch);  
};  
$reducer = function ($comandas, $branch, $mapReduce) {  
$mapReduce-\>emit(array\_sum($comandas), $branch);  
};  
$comandasSum = $this-\>Comandas-\>find()-\>mapReduce($mapper, $reducer);

If you only need one total you could do a cake-y version of your mysql query:

$query = $this-\>Comandas-\>find();  
$comandasSum = $query-\>select([‘RN\_sum’ =\> $query-\>func()-\>sum(‘Comandas.total’)])  
-\>where([‘Comandas.branch’ =\> ‘RN’])  
-\>first();
