简体   繁体   English

MySQL-用第二行的第二列减去第一行的第一列

[英]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 您必须获得小于当前行的stateendtimemax(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.

相关问题 MySQL查询用第二行的第二列减去第一行的第一列 - MySQL Query to Subtract 1st column of 1st row with 2nd column of 2nd row 组表,其中第一行的第一列和lastRow的第二列 - group table with 1st Column of one Row and 2nd Column of lastRow 如果找不到第二表行,如何显示第一行 - how to show 1st row if 2nd table row is not found 获取第1列ON DISTINCT第2列的SUM - Get SUM of 1st column ON DISTINCT 2nd column MySQL根据第一列对第二列和第三列进行排序 - MySQL Sort 2nd and 3rd Column based on the 1st Column MySQL:按第二列排序——如果不为空——同时保持第一列顺序 - MySQL: Order by 2nd column--if not null--while maintaining 1st column order 使用LIMIT函数显示第一行,第二行和第三行,使用MySQL的MAX客户评分 - Using the LIMIT function to display the 1st, 2nd and 3rd row, with the MAX customer rating using MySQL Mysql - 如何根据第一个表的 2 列的值显示第二个表中的列? - Mysql - How to display column from 2nd table based on values from 2 columns of 1st table? 如何使用PHP偏移HTML表以从不同列的第二行单元格值中减去第一行的单元格值 - How To offset an HTML table with PHP to Subtract cell value of 1st row from 2nd row cell value of different columns SQL ORDER BY 第一列,但如果第一列为空,则用第二列中的值替换空值 - SQL ORDER BY 1st column, but if 1st column is empty replace the empty value by a value from 2nd column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM