Variable stay rounding and spoil calculation

Asked

Viewed 80 times

1

I have the following SELECT:

SET @peso := 0;
SET @ganho := 0;

SELECT
    @peso := (SELECT SUM(peso)/1000 FROM entrada WHERE entrada_id = A.entrada_id)+@ganho AS peso,
    @peso,
    @ganho := (SELECT SUM(ganho)/1000 FROM entrada WHERE entrada_id = A.entrada_id) AS ganho,
    @ganho
FROM
    entrada AS A
WHERE
    A.entrada_id IN (18, 19,20, 21,22)
GROUP BY
    A.entrada_id

I start the "weight" and the "gain" with 0 and I have to apply the sum of the weight + gain to each line that is coming back.

My problems are two:

  1. The variable when I place in a column only shows the result as an integer and not as a decimal, I need the decimal pro calculo.

  2. I think since it’s not returning the decimal, it’s messing up the calculation.

Does anyone know why MYSQL round the variable in the column since the result is a decimal?

Result of the query below:

 peso           @peso  ganho        @ganho
 852.1890056    852    0.031025076  0
 852.1889198    852    1.714096448  1
 853.8719912    854    2.465015896  2
 854.6229106    856    2.83135176   2
  • The variables are decimal or float?

  • You are declaring viable as float or real?

  • @Ricardopunctual I’m declaiming at the beginning of the select with: SET @ weight := 0 ; SET @ gain := 0;

  • @Luizsantos At the beginning of my select I do it using SET. When you talk about float or real, could you give me an example? Thanks.

  • data, text la please

1 answer

2


Solution:

SET @in_weight = CAST(0 AS decimal(10,10));
SET @daily_gain := CAST(0 AS decimal(10,10)) ;
  • good!!! : ) sorry about the previous confusion

  • 1

    @Luizsantos Relax. All right. Thank you for your attention. Hug.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.