Mysql:累加计算

Mysql:累加计算_第1张图片
Paste_Image.png

在mysql中实现如上数据累加效果,可使用set,定义用户变量。

sql 语句:

SET @x = 0;
SELECT
  t1.months,
  t1.register_num ,
  @x := @x+t1.register_num AS total
FROM
  (
   SELECT
      DATE_FORMAT(createTime, "%Y%m") months,
      count(id) AS register_num
   FROM
      t_user
   GROUP BY
      months asc
  ) t1

你可能感兴趣的:(Mysql:累加计算)