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 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题