<tfoot id='IrPte'></tfoot>

      <bdo id='IrPte'></bdo><ul id='IrPte'></ul>

      <i id='IrPte'><tr id='IrPte'><dt id='IrPte'><q id='IrPte'><span id='IrPte'><b id='IrPte'><form id='IrPte'><ins id='IrPte'></ins><ul id='IrPte'></ul><sub id='IrPte'></sub></form><legend id='IrPte'></legend><bdo id='IrPte'><pre id='IrPte'><center id='IrPte'></center></pre></bdo></b><th id='IrPte'></th></span></q></dt></tr></i><div id='IrPte'><tfoot id='IrPte'></tfoot><dl id='IrPte'><fieldset id='IrPte'></fieldset></dl></div>

      <small id='IrPte'></small><noframes id='IrPte'>

    1. <legend id='IrPte'><style id='IrPte'><dir id='IrPte'><q id='IrPte'></q></dir></style></legend>
      1. 在 MySQL 中计算运行总数

        时间:2023-08-20
        <legend id='GbA6q'><style id='GbA6q'><dir id='GbA6q'><q id='GbA6q'></q></dir></style></legend>
      2. <small id='GbA6q'></small><noframes id='GbA6q'>

          1. <i id='GbA6q'><tr id='GbA6q'><dt id='GbA6q'><q id='GbA6q'><span id='GbA6q'><b id='GbA6q'><form id='GbA6q'><ins id='GbA6q'></ins><ul id='GbA6q'></ul><sub id='GbA6q'></sub></form><legend id='GbA6q'></legend><bdo id='GbA6q'><pre id='GbA6q'><center id='GbA6q'></center></pre></bdo></b><th id='GbA6q'></th></span></q></dt></tr></i><div id='GbA6q'><tfoot id='GbA6q'></tfoot><dl id='GbA6q'><fieldset id='GbA6q'></fieldset></dl></div>
              <tbody id='GbA6q'></tbody>

            • <tfoot id='GbA6q'></tfoot>

                • <bdo id='GbA6q'></bdo><ul id='GbA6q'></ul>
                  本文介绍了在 MySQL 中计算运行总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有这个 MySQL 查询:

                  I have this MySQL query:

                  SELECT DAYOFYEAR(`date`)  AS d, COUNT(*) 
                  FROM  `orders` 
                  WHERE  `hasPaid` > 0
                  GROUP  BY d
                  ORDER  BY d
                  

                  返回如下内容:

                  d  | COUNT(*) |
                  20 |  5       |
                  21 |  7       |
                  22 | 12       |
                  23 |  4       |
                  

                  我真正想要的是最后的另一列来显示运行总数:

                  What I'd really like is another column on the end to show the running total:

                  d  | COUNT(*) | ??? |
                  20 |  5       |   5 |
                  21 |  7       |  12 |
                  22 | 12       |  24 |
                  23 |  4       |  28 |
                  

                  这可能吗?

                  推荐答案

                  也许对您来说是一个更简单的解决方案,并且可以防止数据库执行大量查询.这仅执行一个查询,然后在一次传递中对结果进行一些数学运算.

                  Perhaps a simpler solution for you and prevents the database having to do a ton of queries. This executes just one query then does a little math on the results in a single pass.

                  SET @runtot:=0;
                  SELECT
                     q1.d,
                     q1.c,
                     (@runtot := @runtot + q1.c) AS rt
                  FROM
                     (SELECT
                         DAYOFYEAR(`date`) AS d,
                         COUNT(*) AS c
                      FROM  `orders`
                      WHERE  `hasPaid` > 0
                      GROUP  BY d
                      ORDER  BY d) AS q1
                  

                  这将为您提供一个额外的 RT(运行总计)列.不要错过顶部的 SET 语句来首先初始化运行总计变量,否则您只会得到一列 NULL 值.

                  This will give you an additional RT (running total) column. Don't miss the SET statement at the top to initialize the running total variable first or you will just get a column of NULL values.

                  这篇关于在 MySQL 中计算运行总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:MySQL参数化查询 下一篇:MySQL - 在选择时获取行号

                  相关文章

                  最新文章

                      <bdo id='wy8Sr'></bdo><ul id='wy8Sr'></ul>

                      <i id='wy8Sr'><tr id='wy8Sr'><dt id='wy8Sr'><q id='wy8Sr'><span id='wy8Sr'><b id='wy8Sr'><form id='wy8Sr'><ins id='wy8Sr'></ins><ul id='wy8Sr'></ul><sub id='wy8Sr'></sub></form><legend id='wy8Sr'></legend><bdo id='wy8Sr'><pre id='wy8Sr'><center id='wy8Sr'></center></pre></bdo></b><th id='wy8Sr'></th></span></q></dt></tr></i><div id='wy8Sr'><tfoot id='wy8Sr'></tfoot><dl id='wy8Sr'><fieldset id='wy8Sr'></fieldset></dl></div>
                      <legend id='wy8Sr'><style id='wy8Sr'><dir id='wy8Sr'><q id='wy8Sr'></q></dir></style></legend>

                      <tfoot id='wy8Sr'></tfoot>

                    1. <small id='wy8Sr'></small><noframes id='wy8Sr'>