У меня есть таблица с некоторыми результатами гонки
CREATE TABLE `fls119_ergebnisse` (
`start_nr` varchar(10) NOT NULL DEFAULT '0',
`rennen` int(11) NOT NULL DEFAULT '-1',
`zeit1` time(3) NOT NULL DEFAULT '00:00:00.000',
`zeit2` time(3) NOT NULL DEFAULT '00:00:00.000',
`zeit3` time(3) NOT NULL DEFAULT '00:00:00.000',
`zeit4` time(3) NOT NULL DEFAULT '00:00:00.000',
`zeit5` time(3) NOT NULL DEFAULT '00:00:00.000',
`zeit6` time(3) NOT NULL DEFAULT '00:00:00.000',
`zeit7` time(3) NOT NULL DEFAULT '00:00:00.000'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `fls119_ergebnisse` (`start_nr`, `rennen`, `zeit1`, `zeit2`, `zeit3`, `zeit4`, `zeit5`, `zeit6`, `zeit7`) VALUES
('0001', 21, '08:59:59.000', '09:03:59.000', '09:08:04.000', '09:12:11.000', '09:16:19.000', '09:20:32.000', '09:24:36.000'),
('0002', 21, '09:00:59.000', '09:04:59.000', '09:09:03.000', '09:13:08.000', '09:17:15.000', '09:21:28.000', '09:25:33.000'),
('0003', 21, '09:02:00.000', '09:06:03.000', '09:10:07.000', '09:14:12.000', '09:18:20.000', '09:22:33.000', '09:26:41.000'),
('0004', 21, '09:03:00.000', '09:06:59.000', '09:11:09.000', '09:15:17.000', '09:19:29.000', '09:23:45.000', '09:27:55.000'),
('0005', 21, '09:03:59.000', '09:08:09.000', '09:12:13.000', '09:16:15.000', '09:20:19.000', '09:24:30.000', '09:28:35.000'),
('0006', 21, '09:05:00.000', '09:09:01.000', '09:12:59.000', '09:16:56.000', '09:20:57.000', '09:25:02.000', '09:28:59.000'),
('0007', 21, '09:05:59.000', '09:10:13.000', '09:14:28.000', '09:18:42.000', '09:23:00.000', '09:27:22.000', '09:31:36.000'),
('0008', 21, '09:06:59.000', '09:11:09.000', '09:15:20.000', '09:19:34.000', '09:23:49.000', '09:28:09.000', '09:32:19.000'),
('0009', 21, '09:07:59.000', '09:12:05.000', '09:16:09.000', '09:20:14.000', '09:24:23.000', '09:28:37.000', '09:32:48.000'),
('0010', 21, '09:08:59.000', '09:13:23.000', '09:17:51.000', '09:22:19.000', '09:26:43.000', '09:31:09.000', '09:35:24.000'),
('0011', 21, '00:00:00.000', '00:00:00.000', '00:00:00.000', '00:00:00.000', '00:00:00.000', '00:00:00.000', '00:00:00.000'),
('0012', 21, '09:10:59.000', '09:15:10.000', '09:19:24.000', '09:23:33.000', '09:27:43.000', '09:32:00.000', '09:36:12.000'),
('0013', -1, '00:00:00.000', '00:00:00.000', '00:00:00.000', '00:00:00.000', '00:00:00.000', '00:00:00.000', '00:00:00.000'),
('0014', -1, '09:19:59.000', '09:23:36.000', '09:27:15.000', '09:30:55.000', '09:34:36.000', '09:38:19.000', '09:41:53.000'),
('0015', -1, '09:20:59.000', '09:24:36.000', '09:28:21.000', '09:32:05.000', '09:35:48.000', '09:39:35.000', '09:43:17.000'),
('0016', -1, '09:21:59.000', '09:25:33.000', '09:29:14.000', '09:32:52.000', '09:36:28.000', '09:40:11.000', '09:43:39.000'),
('0017', -1, '09:22:59.000', '09:26:31.000', '09:30:11.000', '09:33:49.000', '09:37:27.000', '09:41:12.000', '09:44:49.000'),
('0018', -1, '09:23:59.000', '09:27:32.000', '09:31:15.000', '09:34:58.000', '09:38:41.000', '09:42:30.000', '09:46:09.000'),
('0019', -1, '09:24:59.000', '09:28:45.000', '09:32:34.000', '09:36:19.000', '09:40:03.000', '09:43:54.000', '09:47:35.000'),
('0020', -1, '00:00:00.000', '00:00:00.000', '00:00:00.000', '00:00:00.000', '00:00:00.000', '00:00:00.000', '00:00:00.000');
и
ALTER TABLE `fls119_ergebnisse`
ADD PRIMARY KEY (`start_nr`);
COMMIT;
Я хочу сохранить полученное место в другой таблице с
DROP FUNCTION IF EXISTS FortyTwo;
DELIMITER //
SET @place = 1//
SET @times = cast('00:00:00.000' as TIME(3))//
SET @y = 1//
CREATE FUNCTION `FortyTwo`(xtime TIME(3), xstartnr varchar(10)) RETURNS INT
BEGIN
DECLARE x INT;
IF @times = xtime THEN
SET x = @place;
ELSE
SET @place = @y;
SET x = @place;
SET @times = xtime;
END IF;
SET @y = @y +1;
insert into fls119_platzierung (start_nr, platz, zeit) VALUES (xstartnr, x, xtime) on duplicate key update platz = x, zeit = xtime;
RETURN x;
END//
DELIMITER ;
select FortyTwo(timediff(zeit7, zeit1), start_nr), start_nr, timediff(zeit7, zeit1) from fls119_ergebnisse erg where timediff(zeit7, zeit1) > 0 and rennen = 21
ORDER BY timediff(zeit7, zeit1) asc
Результат этого вызова выглядит правильно:
- 0006 00: 23: 59.000
- 0002 00: 24: 34.000
- 0005 00: 24: 36.000
- 0001 00: 24: 37.000
- 0003 00: 24: 41.000
и так далее.Но вызов «select * from fls119_platzierung» дает мне место 12 для номера 6 вместо 1