在Mysql Query

时间:2017-02-23 07:31:03

标签: mysql

我有一张表: -

 ----------------+--------------+--------------+--------------------+
 | IsFirstChunk | BufferedTime       | ElapsedTime | TotalBufferTime|
 +----+--------------+----------------+--------------+--------------+
 | Yes          | 9.056693603959156  | 13.434      | NULL            |
 | NULL         | 8.397168452045355  | 13.915      | NULL            |
 | NULL         | 9.480674058760885  | 14.576      | NULL            |
 | NULL         | 8.25989921902355   | 15.816      | NULL            |
 | NULL         | 9.029792805273495  | 18.573      | NULL            |
 | NULL         | 9.55641146650022   | 26.385      | NULL            |
 | NULL         | 7.567281197192748  | 37.274      | NULL            |
 | NULL         | 9.046965152922192  | 46.142      | NULL            |
 | NULL         | 8.135399698570493  | 56.825      | NULL            |

我想计算BufferTime和ElapsedTime列,并将该输出插入TotalBufferTime列,但我想将输出记录在TotalBufferTime列下面不在相邻行中的一行中。

我想做这样的事情: -

update RequestInfo SET `TotalBufferTime` = BufferedTime + ElapsedTime;

唯一的问题是我希望记录TotalBufferTime

下面一行的输出
+--------------+--------------+--------------+--------------------+
| IsFirstChunk | BufferedTime       | ElapsedTime | TotalBufferTime|
+----+--------------+----------------+--------------+--------------+
| Yes          | 9.056693603959156  | 13.434      | 0              |
| NULL         | 8.397168452045355  | 13.915      | 22.4906936     |
| NULL         | 9.480674058760885  | 14.576      | 22.31216845    |   

1 个答案:

答案 0 :(得分:1)

试试这个:

update RequestInfo join (
    select curr.id, (
        select (prior.BufferedTime + prior.ElapsedTime) AS value  
          from RequestInfo prior
         where prior.id < curr.id
      order by prior.id desc
       limit 1) value
      from RequestInfo curr
  ) g on g.id = RequestInfo.id
set RequestInfo.TotalBufferTime = g.value;