内部存储功能插入失败

时间:2019-04-02 13:00:02

标签: mysql function insert

我有一张桌子,上面有一些比赛结果

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

该调用的结果看起来正确:

  1. 0006 00:23:59.000
  2. 0002 00:24:34.000
  3. 0005 00:24:36.000
  4. 0001 00:24:37.000
  5. 0003 00:24:41.000

,依此类推。但是,调用“ select * from fls119_platzierung”会给我6的位置12而不是1

3 个答案:

答案 0 :(得分:0)

您应该在存储的例程中使用局部变量(declare v_variable datatype),而不要使用用户定义的变量(@variable)。用户定义的变量会在整个过程中保持其值,在您的示例中,变量的初始化不在例程之外,从而导致意外结果。

答案 1 :(得分:0)

感谢您的回答。 如果我使用局部变量,我该如何保留最后的地方号码? 我必须区分两个参与者是否有相同的最后时间,他们到达的是同一地点(例如,第三名)。下一个获得第五名,所以不包括第四名...

不确定,变量初始化是问题所在。 我认为初始化有效。我只是想知道start_6是用位置12而不是1更新的。FortyTwo的返回值给了我正确的结果。所以第一名是6,依此类推。

答案 2 :(得分:0)

我删除了fls119_platzierung上的主索引。 因此数字“ 0006”有两个条目!一个正确的位置为“ 1”,另一个正确的位置为“ 12”。 因此,问题有所改变。为什么第二次执行我的select语句中的第一条记录?