I have a table called Session in MySQL which looks like this:
+-----+---------------------+---------------------+--------------------+
| 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...
I want to create a query which will select all columns in the table, with an additional column showing the next id on the same day (if any) when ordered by Start.
The result would look like this:
+-----+--------+---------------------+---------------------+--------------------+
| 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...
Any suggestions would be appreciated...