MySQL SELECT function to sum current data

后端 未结 3 400
醉酒成梦
醉酒成梦 2020-12-21 12:11

I have a query that return something like this:

| ID | Val |  
| 0  | 10  |     
| 1  | 20  |     
| 2  | 30  |  

But instead of that, I wa

3条回答
  •  鱼传尺愫
    2020-12-21 12:54

    Would something like this work for your purposes? (Warning, potentially really darned slow with the subselect).

    SELECT t1.id, t1.val, (SELECT SUM(val) FROM table AS t2 WHERE t2.id <= t1.id) 'sum'
       FROM table AS t1
       ORDER BY id ASC
    

提交回复
热议问题