我在 MySQL 中有一个名为 Session 的表,如下所示:
+-----+---------------------+---------------------+--------------------+
| id | start | finish | name |
+-----+---------------------+---------------------+--------------------+
| -12 | 2013-04-27 09:00:00 | 2013-04-27 13:00:00 | Saturday Setup 1 |
| -11 | 2013-04-27 13:00:00 | 2013-04-27 18:00:00 | Saturday Setup 2 |
| -10 | 2013-04-27 23:00:00 | 2013-04-28 08:00:00 | Saturday Night |
| -3 | 2013-04-28 08:00:00 | 2013-04-28 13:00:00 | Sunday Setup 1 |
| -2 | 2013-04-28 13:00:00 | 2013-04-28 18:00:00 | Sunday Setup 2 |
| -1 | 2013-04-28 23:00:00 | 2013-04-29 08:00:00 | Sunday Night |
| 1 | 2013-04-29 09:00:00 | 2013-04-29 13:00:00 | Monday Setup 1 |
| 2 | 2013-04-29 13:00:00 | 2013-04-29 17:00:00 | Monday Setup 2 |
| 3 | 2013-04-29 17:00:00 | 2013-04-29 21:00:00 | Monday Setup 3 |
| 4 | 2013-04-29 23:00:00 | 2013-04-30 08:00:00 | Monday Night |
| 10 | 2013-04-30 09:00:00 | 2013-04-30 13:00:00 | Tuesday Setup 1 |
| 11 | 2013-04-30 13:00:00 | 2013-04-30 17:00:00 | Tuesday Setup 2 |
| 12 | 2013-04-30 23:00:00 | 2013-05-01 08:00:00 | Tuesday Night |
more rows etc...
我想创建一个查询,它将选择表中的所有列,并在按 Start 排序时在同一天(如果有)显示下一个 id 的附加列。
结果将如下所示:
+-----+--------+---------------------+---------------------+--------------------+
| id | nextid | start | finish | name |
+-----+--------+---------------------+---------------------+--------------------+
| -12 | -11 | 2013-04-27 09:00:00 | 2013-04-27 13:00:00 | Saturday Setup 1 |
| -11 | -10 | 2013-04-27 13:00:00 | 2013-04-27 18:00:00 | Saturday Setup 2 |
| -10 | | 2013-04-27 23:00:00 | 2013-04-28 08:00:00 | Saturday Night |
| -3 | -2 | 2013-04-28 08:00:00 | 2013-04-28 13:00:00 | Sunday Setup 1 |
| -2 | -1 | 2013-04-28 13:00:00 | 2013-04-28 18:00:00 | Sunday Setup 2 |
| -1 | | 2013-04-28 23:00:00 | 2013-04-29 08:00:00 | Sunday Night |
| 1 | 2 | 2013-04-29 09:00:00 | 2013-04-29 13:00:00 | Monday Setup 1 |
| 2 | 3 | 2013-04-29 13:00:00 | 2013-04-29 17:00:00 | Monday Setup 2 |
| 3 | 4 | 2013-04-29 17:00:00 | 2013-04-29 21:00:00 | Monday Setup 3 |
| 4 | | 2013-04-29 23:00:00 | 2013-04-30 08:00:00 | Monday Night |
| 10 | 11 | 2013-04-30 09:00:00 | 2013-04-30 13:00:00 | Tuesday Setup 1 |
| 11 | 12 | 2013-04-30 13:00:00 | 2013-04-30 17:00:00 | Tuesday Setup 2 |
| 12 | | 2013-04-30 23:00:00 | 2013-05-01 08:00:00 | Tuesday Night |
more rows etc...
任何建议,将不胜感激...