在 MySQL Workbench 中,我有一个以下格式的列表:
ID | type | order | time
----|------|-------|----------
id1 | 1 | 1 | 2017-1-1
id1 | 2 | 2 | 2017-1-2
id1 | 1 | 3 | 2017-1-4
id1 | 2 | 4 | 2017-1-5
id2 | 1 | 1 | 2017-1-1
id2 | 2 | 2 | 2017-1-9
id3 | 1 | 1 | 2017-1-1
id3 | 2 | 2 | 2017-1-2
id3 | 1 | 3 | 2017-1-9
id3 | 2 | 4 | 2017-1-11
id3 | 1 | 5 | 2017-1-13
id3 | 2 | 6 | 2017-1-18
我想重新排序表格以提供以下输出:
ID | time(1) | time(2)
----|-----------|-----------
id1 | 2017-1-1 | 2017-1-2
id1 | 2017-1-4 | 2017-1-5
id2 | 2017-1-1 | 2017-1-9
id3 | 2017-1-1 | 2017-1-2
id3 | 2017-1-9 | 2017-1-11
id3 | 2017-1-13 | 2017-1-18
我有一个针对上述问题的解决方案,但在获取 50 万个条目表上的结果时似乎需要很长时间。我想知道是否有人可以提供我可以尝试的上述问题的解决方案。
谢谢你的帮助,卢卡
最佳答案
select t1.ID, t1.time as 'time(1)', t2.time as 'time(2)'
from Table1 t1 join Table1 t2
on t1.ID=t2.ID and t1.type=1 and t2.type=2
and t1.order=t2.order-1 and t2.order%2=0;
| ID | time(1) | time(2) |
|-----|-----------|-----------|
| id1 | 2017-1-1 | 2017-1-2 |
| id1 | 2017-1-4 | 2017-1-5 |
| id2 | 2017-1-1 | 2017-1-9 |
| id3 | 2017-1-1 | 2017-1-2 |
| id3 | 2017-1-9 | 2017-1-11 |
| id3 | 2017-1-13 | 2017-1-18 |
关于MySQL 重新排序列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44939072/