• <small id='F8Ipg'></small><noframes id='F8Ipg'>

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

      <legend id='F8Ipg'><style id='F8Ipg'><dir id='F8Ipg'><q id='F8Ipg'></q></dir></style></legend>

      1. 如何在 MySQL 中滞后列?

        时间:2023-06-03

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

          1. <tfoot id='rRl9t'></tfoot>
            1. <small id='rRl9t'></small><noframes id='rRl9t'>

                <legend id='rRl9t'><style id='rRl9t'><dir id='rRl9t'><q id='rRl9t'></q></dir></style></legend>

                  <tbody id='rRl9t'></tbody>
                • <bdo id='rRl9t'></bdo><ul id='rRl9t'></ul>

                  本文介绍了如何在 MySQL 中滞后列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  考虑下表:

                  SELECT id, value FROM table ORDER BY id ASC;
                  +-----+---------+
                  | id  | value   |
                  +-----+---------+
                  | 12  | 158     |
                  | 15  | 346     |
                  | 27  | 334     |
                  | 84  | 378     |
                  | 85  | 546     |
                  +-----+---------+
                  

                  id 列是自动递增的,但包含间隙.value 列是数字.

                  The id column is auto-incremented but contains gaps. The value column is numeric.

                  我想通过设置与上面两行 value 相关的 value 来查看 value 随时间的增加.那是对于行 id=85 我想设置与 value 相关的行 id=85 (546) 的 valueid=27 (334).因此,要为 id=85 行计算的值是 546/334=1.63473.

                  I want to look at the increase in value over time by setting value in relation to the value two rows above. That is for row id=85 I want to set the value of row id=85 (546) in relation to the value of row id=27 (334). The value to be computed for row id=85 is hence 546/334=1.63473.

                  这是我想要达到的结果:

                  This is the result I want to achieve:

                  SELECT id, value, ...;
                  +-----+---------+---------------------+
                  | id  | value   | value/lag(value, 2) | (the syntax value/lag(value, 2) is made up)
                  +-----+---------+---------------------+
                  | 12  | 158     | NULL                |
                  | 15  | 346     | NULL                |
                  | 27  | 334     | 2.11392             | (334/158=2.11392)
                  | 84  | 378     | 1.09248             | (378/346=1.09248)
                  | 85  | 546     | 1.63473             | (546/334=1.63473)
                  +-----+---------+---------------------+
                  

                  如何在 MySQL 中执行这种滞后?

                  How do I perform such lagging in MySQL?

                  请注意,id 列包含间隙,因此仅使用 t1.id = t2.id - 2 加入同一个表是行不通的.

                  Please note that the id column contains gaps, so simply joining on the same table with t1.id = t2.id - 2 will not work.

                  推荐答案

                  这里有一个解决方案,在MySQL中返回你想要的内容

                  Here is a solution that returns what you want in MySQL

                  SET @a :=0;
                  SET @b :=2;
                  SELECT r.id, r.value, r.value/r2.value AS 'lag'
                  FROM
                  (SELECT if(@a, @a:=@a+1, @a:=1) as rownum, id, value FROM results) AS r
                  LEFT JOIN
                  (SELECT if(@b, @b:=@b+1, @b:=1) as rownum, id, value FROM results) AS r2
                  ON r.rownum = r2.rownum
                  

                  MySQL 5.1 不喜欢针对子查询的自联接,因此您必须对行进行两次计数,因此不像它可能的那样整洁或可扩展,但它确实使指定滞后变得简单.

                  MySQL 5.1 doesn't like a self join against a subquery so you have to count rows twice, so not as tidy or scalable as it might be, but it does make specifying the lag simple.

                  对于使用 Oracle 的读者来说,这更容易

                  For readers that use Oracle instead this is much easier

                  SELECT id, value, value/lag(value, 2) over (order by id) as lag from results;
                  

                  这篇关于如何在 MySQL 中滞后列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                        <bdo id='Cdk71'></bdo><ul id='Cdk71'></ul>
                          <tbody id='Cdk71'></tbody>