行之间的timediff

时间:2018-01-11 15:58:10

标签: php mysql sql datetime time

我必须按照 fig.1 下面的查询进行操作,但是我想要的是获取第4行和第5行之间的timediff,然后得到第5行和第6行之间的差异

目标是得到类似图2 的东西理想情况下我想在查询中执行此操作而不必循环遍历数据库中的每一行然后返回PHP

提前致谢。

图1

select * from ModuleFlowModuleStatus where ModuleCode = "LW2205" ORDER BY UpdatedOn;


| ID  | Module | MoudleStatus            | UpdatedOn           |

|   4 | LW2205 | Draft exam received     | 2017-10-18 12:41:12 |

|   5 | LW2205 | Draft exam received     | 2017-10-18 12:41:23 |

|   7 | LW2205 | Draft exam received     | 2017-10-20 15:06:46 |

| 275 | LW2205 | Exam approved by Dean   | 2017-11-14 16:39:28 |

| 288 | LW2205 | Final exam sign off by  | 2017-11-21 12:28:59 |

| 295 | LW2205 | Exam sent to SREO (Stud | 2017-11-23 09:53:30 |

+-----+--------+-------------------------+---------------------+

图。 2预期结果

| ID  | Module | MoudleStatus            | UpdatedOn           | Diff(days)

|   4 | LW2205 | Draft exam received     | 2017-10-18 12:41:12 | 0

|   5 | LW2205 | Draft exam received     | 2017-10-18 12:41:23 | 0

|   7 | LW2205 | Draft exam received     | 2017-10-20 15:06:46 | 2

| 275 | LW2205 | Exam approved by Dean   | 2017-11-14 16:39:28 | 24

| 288 | LW2205 | Final exam sign off by  | 2017-11-21 12:28:59 | 7

| 295 | LW2205 | Exam sent to SREO (Stud | 2017-11-23 09:53:30 | 3

+-----+--------+-------------------------+---------------------+

2 个答案:

答案 0 :(得分:1)

此SQL代码适用于您应用于图1中的表:

SELECT
  t1.ID,
  t1.Module,
  t1.MoudleStatus,
  t1.UpdatedOn,
  IFNULL(DATEDIFF(
    t1.UpdatedOn,
    (SELECT MAX(t2.UpdatedOn) FROM ModuleFlowModuleStatus AS t2 WHERE t2.id < t1.id)
  ), 0) AS `Diff(Days)`
FROM
  ModuleFlowModuleStatus AS t1;

代码可以稍微优化/改进,但它适用于我。我省略了你的WHERE和ORDER BY子句来简化我的代码,你只需要再次添加它们。如果有帮助,请告诉我。哦,MySQL DATEDIFF函数默认返回天数值,我认为这正是你想要的。

答案 1 :(得分:0)

另一种方法是分配行号并加入

SELECT T.*, S.RN1,S.UPDATEDON,
         DATEDIFF(t.UPDATEDON,s.UPDATEDON) DIFF
FROM
(
SELECT T.*, 
         IF(T.MODULE <> @P , @RN:=1,@RN:=@RN+1) RN,
         @P:=T.MODULE P
FROM T
CROSS JOIN (SELECT @RN:=0,@P:='') R
where t.Module = 'LW2205'
order by t.module, t.id
)T 
LEFT JOIN
( 
SELECT T.*, 
         IF(T.MODULE <> @P1 , @RN1:=1,@RN1:=@RN1+1) RN1,
         @P1:=T.MODULE P1
FROM T
CROSS JOIN (SELECT @RN1:=0,@P1:='') R
where t.Module = 'LW2205'
order by t.module, t.id
) S
ON S.RN1 = T.RN - 1 AND S.MODULE = T.MODULE;

结果

+------+--------+-------------------------+---------------------+------+--------+------+---------------------+------+
| ID   | Module | MoudleStatus            | UpdatedOn           | RN   | P      | RN1  | UPDATEDON           | DIFF |
+------+--------+-------------------------+---------------------+------+--------+------+---------------------+------+
|    4 | LW2205 | Draft exam received     | 2017-10-18 12:41:12 |    1 | LW2205 | NULL | NULL                | NULL |
|    5 | LW2205 | Draft exam received     | 2017-10-18 12:41:23 |    2 | LW2205 |    1 | 2017-10-18 12:41:12 |    0 |
|    7 | LW2205 | Draft exam received     | 2017-10-20 15:06:46 |    3 | LW2205 |    2 | 2017-10-18 12:41:23 |    2 |
|  275 | LW2205 | Exam approved by Dean   | 2017-11-14 16:39:28 |    4 | LW2205 |    3 | 2017-10-20 15:06:46 |   25 |
|  288 | LW2205 | Final exam sign off by  | 2017-11-21 12:28:59 |    5 | LW2205 |    4 | 2017-11-14 16:39:28 |    7 |
|  295 | LW2205 | Exam sent to SREO (Stud | 2017-11-23 09:53:30 |    6 | LW2205 |    5 | 2017-11-21 12:28:59 |    2 |
+------+--------+-------------------------+---------------------+------+--------+------+---------------------+------+
6 rows in set (0.00 sec)

请注意,结果与预期结果不同