0

我想从名为 tbl 的表中的单个 MySQL 日期时间列中选择一个开始列和一个结束列。有什么建议么?

我从什么开始:

+---------------------+
|      date_time      |
+---------------------+
| 2012-03-05 10:15:52 |
| 2012-03-05 10:15:53 |
| 2012-03-05 10:15:54 |
+---------------------+  

想要的结果:

+---------------------+---------------------+
|     start_time      |      end_time       |
+---------------------+---------------------+
| 2012-03-05 10:15:52 | 2012-03-05 10:15:53 |
| 2012-03-05 10:15:53 | 2012-03-05 10:15:54 |
+---------------------+---------------------+
4

2 回答 2

4
SELECT   cur.date_time AS start_time, MIN(nxt.date_time) AS end_time
FROM     my_table AS cur JOIN my_table AS nxt ON nxt.date_time > cur.date_time
GROUP BY cur.date_time

sqlfiddle上查看。

于 2012-10-14T21:55:09.013 回答
0
select end_time.date_time as [start], 
    (select 
    min(date_time) -- select smallest value from

    from da start_time --another table instance which i called start_time

        -- taking datetimes earlier than start 
    where start_time.date_time > end_time.date_time

    ) as [end]

from da end_time -- table instance with start time

--dont take last value (end time for that value will be null)
where 
    end_time.date_time < (select max(date_time) from da)    

我的桌子:

TABLE [da]
Id  date_time
1   2012-03-05 10:15:52.000
2   2012-03-05 10:15:53.000
3   2012-03-05 10:15:54.000

我的结果:

start                   end
2012-03-05 10:15:52.000 2012-03-05 10:15:53.000
2012-03-05 10:15:53.000 2012-03-05 10:15:54.000
于 2012-10-14T22:05:43.500 回答