【问题标题】:MySQL - Subtract 1st column of 1st row with 2nd column of 2nd rowMySQL - 用第 2 行的第 2 列减去第 1 行的第 1 列
【发布时间】:2019-02-02 09:23:29
【问题描述】:

我正在为一个 Mysql 代码而苦苦挣扎,我不知道如何解决这个问题。我有两列时间值,我在 Mysql 查询结果中按 desc 排序。

我需要找出第 2 行 StateEndTime 和第 1 行 StateStartTtime 之间的差异,以此类推,并将其显示在新的行/列中。决赛桌应该是这样的:

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          

【问题讨论】:

  • 你如何正式定义第一行或第二行的含义

标签: mysql sql


【解决方案1】:
SELECT id,startTime,endTime,type,TIMEDIFF(endTime,startTime) as min_df FROM tasks where 1

【讨论】:

    【解决方案2】:

    你必须得到小于当前行的stateendtimemax(stateendtime)
    并从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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-30
      • 2021-07-13
      • 2020-02-03
      • 1970-01-01
      • 2021-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多