-3

My question is simple, i have a database with 20 records but i want to fetch from the 5th to the 10th the name of the column is amount this is what i wish to achieve below.

the list below is what i will like to fetch from the database

id   |   Amount   | 
 5        10000
 6        5000
 7        10000
 8        12000
 9        5000
10       8000

the list below is what i will like to display

Amount       Balance
10000        10000
 5000         15000
10000        25000
12000        37000
 5000         42000
 8000         50000

so u can see that as it loops through the record, it adds up the next.

Hope you understand what am trying to say??? Thanks

Strawberry
  • 33,750
  • 13
  • 40
  • 57
  • 1
    See http://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-query – Strawberry Jan 22 '17 at 08:32
  • Possible duplicate of [Cumulative sum over a set of rows in mysql](http://stackoverflow.com/questions/17664436/cumulative-sum-over-a-set-of-rows-in-mysql) – chiliNUT Jan 22 '17 at 09:18

1 Answers1

1

You can use user variables to achieve this:

select
    t.*,
    @total := @total + amount balance
from (
    select
        *
    from your_table
    where id between 5 and 10
    order by id    -- important
) t cross join (select @total := 0) t2
Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76