简体   繁体   English

MySQL查询用第二行的第二列减去第一行的第一列

[英]MySQL Query to 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          

Assuming ID is an auto increment column, this can be achieved in MySQL by self join technique: 假设ID是一个自动递增列,则可以在MySQL中通过自连接技术来实现:

 SELECT 
  t1.ID,
  t1.Type,
  t1.StateStartTime,
  t1.StateEndTime
  (t1.StateStartTime - t2.StateEndTime) AS Min_Diff
 FROM
 Table1 t1
    INNER JOIN
 Table1 t2 ON t2.id = t1.id + 1

Is this what you trying to do? 这是您想做的吗?

  mysql_query("SELECT *, `StateEndTime`-`StateStarTtime` AS `Min Difference` 
  FROM `TableName`");

This could be simplified in a MYSQL 8 with CTE and or the ROW_NUM function but this will work to get the solution you are after in most versions. 可以在带有CTE和或ROW_NUM函数的MYSQL 8中简化此操作,但是在大多数版本中,此操作将为您提供所需的解决方案。

I've also casted your times as in the example they arent in the correct format, this may not be the case in your actual data. 我还按照示例说明您的时间,它们以正确的格式显示,但实际数据可能并非如此。

SET @row_number_a = 0;
SET @row_number_b = -1;

SELECT a.id, a.type, a.start, a.end, TIMEDIFF(CAST(a.start AS TIME), CAST(b.end AS TIME)) AS DIFF  
FROM
(SELECT (@row_number_a:=@row_number_a + 1) AS num, id, type, start, end
FROM tbl) a

LEFT JOIN (
SELECT (@row_number_b:=@row_number_b + 1) AS num, id, type, start, end
FROM tbl
) b
ON a.num = b.num

Result: 结果:

"id"    "type"  "start" "end"   "DIFF"
"xxx"   "YYY"   "03:57" "03:59" "00:02:00"
"xxx"   "ZZZ"   "03:53" "03:55" "00:04:00"
"xxx"   "ZZZ"   "03:46" "03:49" \N

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySQL-用第二行的第二列减去第一行的第一列 - MySQL - 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 查询第 1、第 2、第 3 名,使用第 2 和第 3 最低的平局,每次在一行 - query 1st, 2nd, 3rd place with tie break using 2nd and 3rd lowest with each time on one row 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM