To calculate the difference between the current data and the previous data in the same column of mysql
Query data
SELECT a.`STORE_ID` AS id, a.`STORE_NAME` AS NAME, a.`DEPOSIT` AS curr, @a.DEPOSIT AS pre, //deposit value of the previous record @a.DEPOSIT:= a.DEPOSIT AS tmp FROM stock_store_addinfo a,(SELECT @a.DEPOSIT:=0)s
- @a.DEPOSIT:=a.DEPOSIT AS tmp assign the value of a.DEPOSIT to a temporary variable @a.DEPOSIT
- SELECT @a.DEPOSIT:=0 Select the current table, the previous record a.DEPOSIT, the default value is 0
to achieve the difference
SELECT b.id, b.NAME, b.curr, b.pre, (b.curr - b.pre) AS diff FROM ( SELECT a.`STORE_ID` AS id, a.`STORE_NAME` AS NAME, a.`DEPOSIT` AS curr, @a.DEPOSIT AS pre, @a.DEPOSIT:= a.DEPOSIT FROM stock_store_addinfo a,(SELECT @a.DEPOSIT:=0)r )b