mysql中两个连续行之间的差异

编程入门 行业动态 更新时间:2024-10-28 20:25:46
本文介绍了mysql中两个连续行之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在MySQL数据库中有一个表:

I have a table in MySQL database:

id ts number 1 07-10-2017 00:00 200 2 07-10-2017 00:01 300 3 07-10-2017 00:02 700 4 07-10-2017 00:03 1000

我想计算两个连续行之间的差,并且我需要输出格式如下:

I want to calculate difference between two consecutive rows and i need output format be like:

id ts number o/p 1 07-10-2017 00:00 200 0 2 07-10-2017 00:01 300 100 3 07-10-2017 00:02 700 400 4 07-10-2017 00:03 1000 300

我希望o/p列的第一个值为0,其余为差.

I want first value of o/p column to be 0 and rest is the difference.

任何帮助将不胜感激.

推荐答案

您可以使用局部变量来完成此操作:

You can do this with a local variable:

SET @tmp := -1; SELECT id, ts , number, if(@tmp =-1, 0, number - @tmp) as 'o/p', @tmp:=number as dummy FROM YourTable Order by ts;

更多推荐

mysql中两个连续行之间的差异

本文发布于:2023-10-24 06:10:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1523141.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:行之   差异   两个   mysql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!