我在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...
我想创建一个查询,该查询将选择表格中的所有列,其他列显示在按开始排序的同一天(如果有)的下一个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...
任何建议都会受到赞赏......
答案 0 :(得分:1)
它不会非常有效,因为索引不能用于评估自我加入标准DATE(..) = DATE(..)
,但您实际上是在尝试获取groupwise minimum:
SELECT cur.id, nxt.id nextid, cur.start, cur.finish, cur.name
FROM (
SELECT s1.*, MIN(s2.start) next
FROM Session s1
LEFT JOIN Session s2
ON DATE(s1.start) = DATE(s2.start)
AND s1.start < s2.start
GROUP BY s1.start
) cur LEFT JOIN Session nxt ON cur.next = nxt.start
在sqlfiddle上查看。
答案 1 :(得分:1)
我不确定,但你可以试试这个
最后你的MySql应该是5.4 +
答案 2 :(得分:1)
这是我认为相关子查询很有用的地方:
select id,
(select id from session s2 where date(s.start) = date(s2.start) order by start desc limit 1
) as nextId
start, finish, name
from session s
答案 3 :(得分:0)
使用开始日期来确定天数是否匹配,这样的事情应该这样做。一个subselect来获取min id,它比同一天的另一个id大。我不敢测试。
SELECT a.id, Sub1.MinId AS nextid, a.start, a.finish, a.name
FROM SomeTable a
LEFT OUTER JOIN (SELECT a.id, MIN(b.id) AS MinId
FROM SomeTable a
INNER JOIN SomeTable b
ON DATE(a.start) = DATE(b.start)
AND a.id < b.id
GROUP BY a.id) Sub1
ON a.id = Sub1.id
或者id不在开始顺序
SELECT a.id, c.id AS nextid, a.start, a.finish, a.name
FROM SomeTable a
LEFT OUTER JOIN (SELECT a.id, MIN(b.start) AS MinStart
FROM SomeTable a
INNER JOIN SomeTable b
ON DATE(a.start) = DATE(b.start)
AND a.id < b.id
GROUP BY a.id) Sub1
ON a.id = Sub1.id
LEFT OUTER JOIN SomeTable c
ON Sub1.MinStart = c.start