0

完全编辑我的问题。因为我想我的第一次尝试太不清楚了。我实际上有三列的表。我需要添加新列并按从开始经过的时间填充它

|| *station_id* || *counter* || *stop* || *time from begining* ||
|| 22500 || 0 || 16:15:00 ||  ||
|| 22710 || 1 || 17:24:00 ||  ||
|| 22790 || 2 || 18:10:00 ||  ||
|| 22670 || 3 || 19:43:00 ||  ||
|| 22800 || 4 || 21:42:00 ||  ||
|| 22920 || 5 || 22:47:00 ||  ||
|| 24050 || 6 || 00:53:00 ||  ||
|| 24010 || 7 || 02:13:00 ||  ||
|| 24280 || 8 || 02:30:00 ||  ||
|| 24270 || 9 || 02:57:00 ||  ||
|| 24250 || 10 || 03:45:00 ||  ||
|| 24249 || 11 || 06:45:00 ||  ||
|| 25270 || 12 || 08:06:00 ||  ||
|| 1190 || 13 || 09:23:00 ||  ||
|| 25230 || 14 || 10:43:00 ||  ||
|| 25201 || 15 || 11:23:00 ||  ||
|| 25215 || 16 || 11:54:00 ||  ||
|| 24605 || 17 || 13:05:00 ||  ||
|| 24577 || 18 || 13:44:00 ||  ||
|| 24580 || 19 || 14:23:00 ||  ||
|| 24583 || 20 || 15:09:00 ||  ||
|| 24585 || 21 || 15:40:00 ||  ||
|| 24610 || 22 || 16:40:00 ||  ||
|| 24850 || 23 || 17:47:00 ||  ||
|| 24840 || 24 || 18:12:00 ||  ||
|| 24500 || 25 || 21:32:00 ||  ||
|| 24760 || 26 || 00:52:00 ||  ||
|| 24100 || 27 || 06:23:00 ||  ||
|| 24791 || 28 || 08:55:00 ||  ||
|| 24938 || 29 || 10:05:00 ||  ||
|| 24943 || 30 || 10:34:00 ||  ||
|| 21602 || 31 || 11:52:00 ||  ||
|| 21609 || 32 || 14:58:00 ||  ||
|| 21800 || 33 || 16:32:00 ||  ||
|| 21630 || 34 || 17:13:00 ||  ||
|| 21646 || 35 || 18:02:00 ||  ||
|| 21640 || 36 || 18:55:00 ||  ||
|| 21650 || 37 || 19:15:00 ||  ||
|| 21670 || 38 || 20:38:00 ||  ||
|| 21654 || 39 || 21:13:00 ||  ||
|| 21680 || 40 || 21:35:00 ||  ||
|| 21658 || 41 || 22:39:00 ||  ||
|| 21600 || 42 || 01:11:00 ||  ||
|| 21760 || 43 || 03:09:00 ||  ||
|| 27580 || 44 || 04:43:00 ||  ||
|| 27453 || 45 || 05:14:00 ||  ||
|| 27570 || 46 || 05:34:00 ||  ||
|| 27560 || 47 || 06:22:00 ||  ||
|| 27438 || 48 || 06:52:00 ||  ||
|| 27550 || 49 || 07:53:00 ||  ||
|| 27530 || 50 || 09:04:00 ||  ||
|| 27200 || 51 || 11:14:00 ||  ||
|| 27240 || 52 || 14:08:00 ||  ||

如何更新它,并提出最后一个问题,从计数器 = 0 的车站过去了多少分钟。

我的错误尝试如下所示:

select station_id,counter, stop_ends,
IF(TIMEDIFF(stop_ends, @diff := (select (stop_ends) from route ri where ri.train_id=ro.train_id and counter=0)) >= 0, TIMEDIFF(stop_ends, @diff),
SEC_TO_TIME(
    TIME_TO_SEC(TIMEDIFF('23:59:59', @diff)) + 1 +
    TIME_TO_SEC(TIMEDIFF(stop_ends, '00:00:00')))
)
as travel
from route ro where train_id = 1130 order by counter

它完美地估计了时间,但仅限于第一天。

4

2 回答 2

0

据我了解,您每天有一个条目,并且您想返回每个条目相对于其他条目的哪一天。

SQL 数据并没有像那样排序。它通常不会根据将数据添加到系统的时间对数据进行排序。除非您有另一列增加了 id 或添加了日期或其他内容,否则您无能为力。

如果您确实有某种递增的 ID 列或添加日期的列,那么您的答案只是一个简单的比较。显示您的架构,这里的任何人都可能会有所帮助。

如果您仍在设计此系统并且需要此功能,请添加一个额外的列。如果您正在处理现有系统和数据集,那么您就是 SOL。

于 2013-08-18T19:40:48.453 回答
0

也许它不是优化但它的工作。

set @prefix := TIME_TO_SEC("00:00:00");

select station_id,counter, stop_ends,
if
(
    (@travel := IF(TIMEDIFF(stop_ends, @diff := (select (stop_ends) from route ri where ri.train_id=ro.train_id and counter=0)) >= 0, TIMEDIFF(stop_ends, @diff),
    SEC_TO_TIME(
        TIME_TO_SEC(TIMEDIFF('23:59:59', @diff)) + 1 +
        TIME_TO_SEC(TIMEDIFF(stop_ends, '00:00:00')))
    ))
    <
    (IF(TIMEDIFF(@diff2 := (select (stop_ends) from route ri2 where ri2.train_id=ro.train_id and counter=ro.counter-1), @diff) >= 0, TIMEDIFF(@diff2, @diff),
    SEC_TO_TIME(
        TIME_TO_SEC(TIMEDIFF('23:59:59', @diff)) + 1 +
        TIME_TO_SEC(TIMEDIFF(@diff2, '00:00:00')))
    )), SEC_TO_TIME(@prefix := @prefix + TIME_TO_SEC('24:00:00') + TIME_TO_SEC(@travel)), SEC_TO_TIME(@prefix + TIME_TO_SEC(@travel))
) as travel
from route ro where train_id = 1130 order by counter
于 2013-08-18T21:42:54.427 回答