1

我想将 3 分钟步长的时间戳值(给定开始和结束日期)插入到只有一个时间戳列的表中。

我目前从 sql server 切换到 mysql。我会知道如何为 SQL 中的前一个数据库执行此操作,但不确定后者。谢谢。

这是我目前(不工作)的悲惨尝试:

DELIMITER //
CREATE PROCEDURE test()
begin
    declare StartTS TIMESTAMP DEFAULT null;
    declare EndTS TIMESTAMP DEFAULT null;

    set StartTS = '2012-01-01 00:00:00';
    set EndTS = '2014-01-01 00:00:00';

    start transaction;
      while StartTS < EndTS do
        insert into timestamps (TimeStampEntry) values (StartTS);
        SET StartTS  = StartTS  + INTERVAL 3 MINUTE;
      end while;
      commit;
end
//

DELIMITER ;

call test();

PS:

create table TimeStamps (
        Id INTEGER NOT NULL AUTO_INCREMENT,
       TimeStampEntry DATETIME,
       primary key (Id)
    )

当前存储过程:

DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `myProc`(IN t1 timestamp, IN t2 timestamp)
BEGIN

  WHILE t1 <= t2 DO
INSERT INTO timestamps (TimeStampEntry) Values (t1)  ;
    SET t1 = DATE_ADD(t1, INTERVAL 180 SECOND);
    #SET t1 = t1 + INTERVAL 180 SECOND;
  END WHILE;
END

称呼:

CALL myProc('2010-01-01 00:00:00', '2014-01-01 00:00:00');
4

1 回答 1

3

尝试这样的事情:

CREATE PROCEDURE myProc(IN t1 timestamp, IN t2 timestamp)
BEGIN

  WHILE t1 <= t2 DO
INSERT INTO test (ts) Values (t1)  ;
    SET t1 = DATE_ADD(t1, INTERVAL 3 MINUTE);
  END WHILE;
END;


CALL myProc(now(), ADDDATE(NOW(), INTERVAL 15 MINUTE));

http://sqlfiddle.com/#!2/5672e/9

或更改您的线路:

SET StartTS  = StartTS  + INTERVAL 3 MINUTE;

SET StartTS  = ADDDATE(StartTS, INTERVAL 3 MINUTE);
于 2012-10-09T14:34:01.410 回答