昨天有人询问哪一个需要使用LAG更新MySQL表。考虑下面的输入表(左),以及所需的输出(右):在更新过程中在MySQL中模拟LAG
**INPUT** **OUTPUT**
ID TestDate PerformanceStatus (PS) ID TestDate PS PreviousPerformanceStatus
1 15/03/2016 0 1 15/03/2016 0 0
1 01/04/2016 2 1 01/04/2016 2 0
1 05/05/2016 1 1 05/05/2016 1 2
1 07/06/2016 1 1 07/06/2016 1 1
2 15/03/2016 0 2 15/03/2016 0 1
2 01/04/2016 2 2 01/04/2016 2 0
2 05/05/2016 1 2 05/05/2016 1 2
2 07/06/2016 3 2 07/06/2016 3 1
2 23/08/2016 1 2 23/08/2016 1 3
换句话说,我们的目标是要分配给PreviousPerformanceStatus
这在来之前记录存在的价值,通过ID
作为排序则TestDate
。
由@ spencer7593给出的接受答案使用了相关的子查询。但是,首先突然想到的是使用用户变量。这里是我回答:
SET @lag = 0;
UPDATE yourTable
SET PreviousPerformanceStatus = @lag,
@lag:=PerformanceStatus
ORDER BY ID, TestDate
有人告诉我,这个答案是不稳定的,但我想知道,如果有人可以解释为什么事情可能出错,你会在这种情况下可以发生的,最后什么事情我们在这里使用用户变量来模拟LAG。
这是我的理解,下面SELECT
查询就没有问题可言:
SELECT PerformanceStatus,
@lag AS PreviousPerformanceStatus,
@lag:=PerformanceStatus
FROM yourTable
ORDER BY ID, TestDate
但是,这样做时UPDATE
有其他方面的考虑考虑。
更新语句抛出一个1064错误,所以这个问题有点没有意义。 –
@ P.Salmon有什么方法可以解决这个错误? –
错误是, @lag:= PerformanceStatus,我不认为你可以在更新语句中设置变量 - 所以不。 –