[英]MySQL Update Running Max
我正在尝试创建一个查询,该查询根据另一列的运行最大值(例如本例中的得分)更新运行的最大列。
查询前表
time-----score---------current max
1-----------5--------------null
2-----------6--------------null
3-----------3--------------null
4-----------4--------------null
5-----------5--------------null
6-----------7--------------null
7-----------8--------------null
查询后表
time-----score---------current max
1-----------5--------------5
2-----------6--------------6
3-----------3--------------6
4-----------4--------------6
5-----------5--------------6
6-----------7--------------7
7-----------8--------------8
任何建议将不胜感激。
select @max := case when score > @max
then score
else @max
end as curr_max,
time,
score
from your_table, (select @max := 0) m
尝试这个:
UPDATE tableA A
INNER JOIN (SELECT A.time, (@temp:=IF(score > @temp, score, @temp)) currentMax
FROM tableA A, (SELECT @temp:=0) B
) B ON A.time = B.time
SET A.currentMax = B.currentMax
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.