mysql implements the subtraction of two adjacent data

   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

 

  1. @a.DEPOSIT:=a.DEPOSIT AS tmp assign the value of a.DEPOSIT to a temporary variable @a.DEPOSIT
  2. 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

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326099413&siteId=291194637