-1
Hello, I’m trying to sum up some values using the SUM
and is returning 0.
The fields I will use:
- category
- month
- value (want to add this)
I want to add the valor
of all records of each category and display next to the same category.
Code:
// SELECT NOS CAMPOS A SEREM EXIBIDOS
$result_transacoes = "SELECT categoria, mes, sum(valor) FROM contas WHERE mes = 'JANEIRO' GROUP BY categoria ORDER BY categoria ASC";
$resultado_trasacoes = mysqli_query($con, $result_transacoes);
while($row_transacoes = mysqli_fetch_assoc($resultado_trasacoes)){
$html .= '<tr><td>'.$row_transacoes['categoria'] . "</td>";
$html .= '<td>'.$row_transacoes['mes'] . "</td>";
$html .= '<td>'.$row_transacoes['sum(valor)'] . "</td>";
}
I started trying to add up the total value of each category, however, it did not work, is returning "0", perhaps because of the R$? What can I do in this case? I tried to use the SUM
.
Hello @Sam, values are being saved with
R$
yes, I removed all theR$
of the column to make a test, but the sum of them did not hit, some were like: 6041.0294810302, and another concern is, how will I put a mask of monetary value on an input without saving the"R$"
?– Samuel
@Sam, I removed the
"R$" "." ","
and put asINT
, apparently I think it worked out the sum, but I’m not able to divide the value by 100, I tried to put in the own select, so:SELECT categoria, mes, sum(valor)/100 FROM contas
– Samuel
Man, it’s a simple thing and I’m not getting... I did it like this:
$valor_dividido = 'sum(valor) / 100';

 $html .= '<td>'.$row_transacoes['$valor_dividido']. "</td>";
– Samuel
It would be right:
$valor_dividido = $row_transacoes['sum(valor)'] / 100;
and here would be:$html .= '<td>'.$valor_dividido. "</td>";
– Sam