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?
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
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: