MySQL累积求和
有如下表
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 cum FROM cum_demo a JOIN cum_demo lt ON a.id >= lt.id GROUP BY a.money ORDER BY id
结果
id | money | cum |
1 | 10 | 10 |
2 | 20 | 30 |
3 | 30 | 60 |
4 | 40 | 100 |
声明:如有转载本博文章,请注明出处。您的支持是我的动力!文章部分内容来自互联网,本人不负任何法律责任。