dpxo13079 2015-05-20 08:23
浏览 100
已采纳

MySql - 从开始到每天计数

Let's say I have the following table (keep in mind that this table will have 10000+ rows):

id    total           date
1       5        2015-05-16
2       8        2015-05-17
3       4        2015-05-18
4       9        2015-05-19
5       3        2015-05-20

I want the query to give the following result:

1
date => 2015-05-16
total => 5

2
date => 2015-05-17
total => 13

3
date => 2015-05-18
total => 17

4
date => 2015-05-19
total => 26

5
date => 2015-05-20
total -> 29

I can't think of any query that would do this right now, that's why I am not providing any code that I have tried.

Any thoughts? I am not sure if this is possible only with mysql, maybe I have to use and php.

  • 写回答

3条回答 默认 最新

  • dongwoqin7034 2015-05-20 08:30
    关注

    This could be done using user defined variable in mysql and then get the running total as

    select
    id,
    total,
    date
    date from
    (
     select
     id,
     @tot:= @tot+total as total,
     date from my_table,(select @tot:=0)x
     order by date
    )x
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应