【发布时间】:2020-05-28 18:13:26
【问题描述】:
我有一列运行总和,但我想在新列或同一列中颠倒顺序
我的查询是
SELECT date,
ant,
num_room,
(@csum:= @csum + num_room) as cumulative_room
from xxx_xxxx_xxxx
WHERE date BETWEEN CURDATE() - INTERVAL 30 DAY AND CURDATE()
AND(select @csum := 0) = 0
order by date
米表是
date | ant | num_room | cumulative_room
28-04-2020 6 1 1
28-04-2020 3 1 2
28-04-2020 4 1 3
28-04-2020 7 1 4
28-04-2020 4 1 5
....
我怎样才能从累积空间做相反顺序的另一个变量?
date | ant | num_room | cumulative_room |reverse_cumulative room
28-04-2020 6 1 1 5
28-04-2020 3 1 2 4
28-04-2020 4 1 3 3
28-04-2020 7 1 4 2
28-04-2020 4 1 5 1
....
【问题讨论】:
-
在 MySQL 8.x 中很容易。您使用的是 MySQL 5.x 还是 8.x?
-
我使用的是 MYSQL 5.6.4
标签: mysql