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
posted @ 2013-08-09 18:51  bourneli  阅读(18144)  评论(0编辑  收藏