我在mysql的 临时 表中有两列case_id
和assigned_date
。因为在mysql中没有直接的函数来获得引导和滞后oracle。想要针对生成的每个案例ID获得指定日期的超前和滞后两个值。如何实现这一目标。随附的是我的样本数据。
case_id assigned_date
41c19f76-e52e-c4c9-62c2-573c71ec5d50 2016-05-18 14:08:14.0
41c19f76-e52e-c4c9-62c2-573c71ec5d51 2016-05-18 14:25:22.0
41c19f76-e52e-c4c9-62c2-573c71ec5d50 2016-05-18 14:26:01.0
41c19f76-e52e-c4c9-62c2-573c71ec5d50 2016-05-19 07:19:13.0
41c19f76-e52e-c4c9-62c2-573c71ec5d50 2016-05-19 07:53:09.0
41c19f76-e52e-c4c9-62c2-573c71ec5d51 2016-05-19 08:18:01.0
41c19f76-e52e-c4c9-62c2-573c71ec5d50 2016-05-19 12:12:35.0
答案 0 :(得分:2)
您可以使用相关的子查询:
SELECT t1.case_id, t1.assigned_date,
(SELECT t2.assigned_date
FROM mytable AS t2
WHERE t2.case_id = t1.case_id AND
t2.assigned_date > t1.assigned_date
ORDER BY t2.assigned_date LIMIT 1) AS next_date,
(SELECT t2.assigned_date
FROM mytable AS t2
WHERE t2.case_id = t1.case_id AND
t2.assigned_date < t1.assigned_date
ORDER BY t2.assigned_date DESC LIMIT 1) AS prev_date
FROM mytable AS t1