How to change a mysql variable inside a query?

I have a table with two columns, namely cumulative_sum and absolute. But I have only cumulative value. I would like to calculate the absolute value.

Cumulative value
12
19
32
41

Expected Absolute Value
12
7
13
9

I tried such a request. I just need to update @absvalue to the end each time the request is updated.

set @absvalue := 0;
update pdb_tint_result set abs = (cum - @absvalue)
where user_id='P6'
order by date;

Can you guide me?

I saw the calculation of the total amount here. I'm trying to get it to work to calculate the value of Abs.

+4
source share
2 answers

- , .

"CREATE TRIGGER update_abs 
 BEFORE UPDATE ON pdb_tint_result
 FOR EACH ROW SET abs = cum - New.absvalue;"

, .

+1

-

SET @prev_cum:=0;
UPDATE test_point_sum 
   SET absolute_value = (cumulative_sum - @prev_cum),
       cumulative_sum = @prev_cum:=cumulative_sum 
   ORDER BY 
      ID ASC;

. . ,

+2

All Articles