[英]MySQL - Subtract 1st column of 1st row with 2nd column of 2nd row
I'm struggling with a Mysql Code, and I have no clue how to solve this. 我正在努力使用Mysql代码,并且不知道如何解决此问题。 I have two columns with time value on which I've ordered by desc in Mysql Query result.
我在Mysql Query结果中按desc排序了两列带有时间值的列。
I need to find out the differences between the 2nd row StateEndTime and the 1st-row StateStarTtime nd so on and display this in a new row/column. 我需要找出第二行StateEndTime和第一行StateStarTtime nd之间的差异,然后将其显示在新行/列中。 The final table should look like this :
决赛桌应如下所示:
ID Type StateStarTtime StateEndTime Min Difference
xxx YYY 03:57 03:59 00:02
xxx ZZZ 03:53 03:55 00:04
xxx ZZZ 03:46 03:49
SELECT id,startTime,endTime,type,TIMEDIFF(endTime,startTime) as min_df FROM tasks where 1
You must get the max(stateendtime)
that is less than the current row's stateendtime
您必须获得小于当前行的
stateendtime
的max(stateendtime)
and subtract it from statestarttime
: 并从
statestarttime
减去它:
select
t.*,
left(timediff(
t.statestarttime,
select max(stateendtime) from tablename where stateendtime < t.stateendtime
), 5) mindifference
from tablename t
order by t.statestarttime desc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.