Use user-defined variables:
SET @prevValue:=0; SELECT value-@prevValue AS diff,@prevValue:=value FROM t;
You will have to discard the first row for your case.
If the table contains both value and diff fields, you can set the diff field with:
SET @prevValue:=0; UPDATE t SET diff=IF((@d:=value-@prevValue) AND (@prevValue:=value),@d,@d);
Using IF is the only way to find how to set diff and update the @prevValue variable.
Roger Dueck
source share