有如下表
id | money |
1 | 10 |
2 | 20 |
3 | 30 |
4 | 40 |
求出如下数据
id | money | cum |
1 | 10 | 10 |
2 | 20 | 30 |
3 | 30 | 60 |
4 | 40 | 100 |
建表
CREATE TABLE cum_demo(id INT,money INT,PRIMARY KEY (id))
插入数据
insert into cum_demo(id,money)values (1,10),(2,20),(3,30),(4.40);
求累加
SELECT a.id,a.money,SUM(lt.money) as cumFROM cum_demo a JOIN cum_demo lt ON a.id >= lt.idGROUP BY a.moneyORDER BY id
结果
id | money | cum |
1 | 10 | 10 |
2 | 20 | 30 |
3 | 30 | 60 |
4 | 40 | 100 |