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

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

    <legend id='xaMRu'><style id='xaMRu'><dir id='xaMRu'><q id='xaMRu'></q></dir></style></legend>
    <tfoot id='xaMRu'></tfoot>
    1. 在 MySQL 中创建累积总和列

      时间:2023-08-20
      <i id='2dXx8'><tr id='2dXx8'><dt id='2dXx8'><q id='2dXx8'><span id='2dXx8'><b id='2dXx8'><form id='2dXx8'><ins id='2dXx8'></ins><ul id='2dXx8'></ul><sub id='2dXx8'></sub></form><legend id='2dXx8'></legend><bdo id='2dXx8'><pre id='2dXx8'><center id='2dXx8'></center></pre></bdo></b><th id='2dXx8'></th></span></q></dt></tr></i><div id='2dXx8'><tfoot id='2dXx8'></tfoot><dl id='2dXx8'><fieldset id='2dXx8'></fieldset></dl></div>

          <tfoot id='2dXx8'></tfoot>
        1. <legend id='2dXx8'><style id='2dXx8'><dir id='2dXx8'><q id='2dXx8'></q></dir></style></legend>
        2. <small id='2dXx8'></small><noframes id='2dXx8'>

            <tbody id='2dXx8'></tbody>

            <bdo id='2dXx8'></bdo><ul id='2dXx8'></ul>

              1. 本文介绍了在 MySQL 中创建累积总和列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我有一张看起来像这样的表格:

                I have a table that looks like this:

                id   count
                1    100
                2    50
                3    10
                

                我想添加一个名为cumulative_sum的新列,因此该表将如下所示:

                I want to add a new column called cumulative_sum, so the table would look like this:

                id   count  cumulative_sum
                1    100    100
                2    50     150
                3    10     160
                

                是否有可以轻松完成此操作的 MySQL 更新语句?实现这一目标的最佳方法是什么?

                Is there a MySQL update statement that can do this easily? What's the best way to accomplish this?

                推荐答案

                如果性能有问题,您可以使用 MySQL 变量:

                If performance is an issue, you could use a MySQL variable:

                set @csum := 0;
                update YourTable
                set cumulative_sum = (@csum := @csum + count)
                order by id;
                

                或者,您可以删除 cumulative_sum 列并在每个查询中计算它:

                Alternatively, you could remove the cumulative_sum column and calculate it on each query:

                set @csum := 0;
                select id, count, (@csum := @csum + count) as cumulative_sum
                from YourTable
                order by id;
                

                这以运行方式计算运行总和:)

                This calculates the running sum in a running way :)

                这篇关于在 MySQL 中创建累积总和列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何将整个 MySQL 数据库字符集和排序规则转换为 下一篇:如何在单个表中存储多个选项?

                相关文章

                最新文章

                <tfoot id='h3LTe'></tfoot>
              2. <legend id='h3LTe'><style id='h3LTe'><dir id='h3LTe'><q id='h3LTe'></q></dir></style></legend>

                • <bdo id='h3LTe'></bdo><ul id='h3LTe'></ul>

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

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