时间维度填充问题

发布于 2024-11-16 07:26:14 字数 637 浏览 2 评论 0原文

我的 MySql 表返回的是天而不是时间。我每天需要最少的分钟数,因此应该自动填充 1440 条记录,但我不断地返回几天。知道为什么吗?另外,我不需要秒,只需要小时和分钟,但我不确定如何在没有秒的情况下填充日期,因为时间格式始终为“00:00:00”

程序如下:

DELIMITER //
CREATE PROCEDURE p_sc_time(IN startdate DATETIME,IN stopdate DATETIME)
BEGIN
DECLARE currentdate DATE;
SET currentdate = startdate;
WHILE currentdate < stopdate DO
    INSERT INTO sc_time VALUES (
    NULL,
    TIME(currentdate),
    HOUR(currentdate),
    MINUTE(currentdate));
SET currentdate == ADDDATE(currentdate,INTERVAL 1 MINUTE);
END WHILE;
END
//
DELIMITER ;
TRUNCATE TABLE sc_time;
CALL p_sc_time('01-01-01 00:00:00','01-01-01 23:59:59');

My MySql table is returning days instead of time. I need a minimum level of minutes in a day so 1440 records should be auto populated but i keep getting days back. Any idea why? Also i dont need seconds, only hours and minutes but i am not sure how to do a date fill without seconds since the format of time is always '00:00:00'

The procedure is below:

DELIMITER //
CREATE PROCEDURE p_sc_time(IN startdate DATETIME,IN stopdate DATETIME)
BEGIN
DECLARE currentdate DATE;
SET currentdate = startdate;
WHILE currentdate < stopdate DO
    INSERT INTO sc_time VALUES (
    NULL,
    TIME(currentdate),
    HOUR(currentdate),
    MINUTE(currentdate));
SET currentdate == ADDDATE(currentdate,INTERVAL 1 MINUTE);
END WHILE;
END
//
DELIMITER ;
TRUNCATE TABLE sc_time;
CALL p_sc_time('01-01-01 00:00:00','01-01-01 23:59:59');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぃ双果 2024-11-23 07:26:14

我没有正在运行的 MySQL 实例来测试,但我相信您应该将当前日期类型更改为 DATETIME。

I don't have a running instance of MySQL to test, but I believe you should change your currentdate type to DATETIME.

素罗衫 2024-11-23 07:26:14
DROP TABLE IF EXISTS timedim;

CREATE TABLE timedim  (
    time_id INT NOT NULL auto_increment,
    fulltime time,
    hour int,
    minute int,
    second int,
    ampm varchar(2),
    PRIMARY KEY(time_id)
) ENGINE=InnoDB AUTO_INCREMENT=1000;

CREATE TABLE IF NOT EXISTS --#Must be a non-temporary table b/c temp tables cannot be self joined.
  ints ( i INT ) AS #(Use select+union for CTAS - Create Table)
      SELECT (0) i UNION SELECT (1) UNION SELECT (2) UNION SELECT (3) UNION SELECT (4)
UNION SELECT (5) UNION SELECT (6) UNION SELECT (7) UNION SELECT (8) UNION SELECT (9);

DROP PROCEDURE IF EXISTS timedimbuild;

CREATE PROCEDURE timedimbuild()
BEGIN

DECLARE v_full_date DATETIME;

CREATE TEMPORARY TABLE filler(id INT NOT NULL);

INSERT INTO filler(id)
SELECT (a.i*10000 + b.i*1000 + c.i*100 + d.i*10 + e.i) id
FROM ints a JOIN ints b JOIN ints c JOIN ints d JOIN ints e
WHERE (a.i*10000 + b.i*1000 + c.i*100 + d.i*10 + e.i) <= 86399 --# 86,400 seconds in a day (0 to 86399)
ORDER BY 1;

SET v_full_date = '1970-01-01 00:00:00';        

INSERT INTO timedim (
    fulltime ,
    hour ,
    minute ,
    second ,
    ampm
) SELECT
     TIME(DATE_ADD(v_full_date, INTERVAL t.id SECOND)), 
     HOUR(DATE_ADD(v_full_date, INTERVAL t.id SECOND)), 
     MINUTE(DATE_ADD(v_full_date, INTERVAL t.id SECOND)), 
     SECOND(DATE_ADD(v_full_date, INTERVAL t.id SECOND)), 
     DATE_FORMAT(DATE_ADD(v_full_date, INTERVAL t.id SECOND), '%p')      
  FROM (SELECT id FROM filler) t; 

DROP TEMPORARY TABLE filler;
END;

call timedimbuild();
DROP TABLE IF EXISTS timedim;

CREATE TABLE timedim  (
    time_id INT NOT NULL auto_increment,
    fulltime time,
    hour int,
    minute int,
    second int,
    ampm varchar(2),
    PRIMARY KEY(time_id)
) ENGINE=InnoDB AUTO_INCREMENT=1000;

CREATE TABLE IF NOT EXISTS --#Must be a non-temporary table b/c temp tables cannot be self joined.
  ints ( i INT ) AS #(Use select+union for CTAS - Create Table)
      SELECT (0) i UNION SELECT (1) UNION SELECT (2) UNION SELECT (3) UNION SELECT (4)
UNION SELECT (5) UNION SELECT (6) UNION SELECT (7) UNION SELECT (8) UNION SELECT (9);

DROP PROCEDURE IF EXISTS timedimbuild;

CREATE PROCEDURE timedimbuild()
BEGIN

DECLARE v_full_date DATETIME;

CREATE TEMPORARY TABLE filler(id INT NOT NULL);

INSERT INTO filler(id)
SELECT (a.i*10000 + b.i*1000 + c.i*100 + d.i*10 + e.i) id
FROM ints a JOIN ints b JOIN ints c JOIN ints d JOIN ints e
WHERE (a.i*10000 + b.i*1000 + c.i*100 + d.i*10 + e.i) <= 86399 --# 86,400 seconds in a day (0 to 86399)
ORDER BY 1;

SET v_full_date = '1970-01-01 00:00:00';        

INSERT INTO timedim (
    fulltime ,
    hour ,
    minute ,
    second ,
    ampm
) SELECT
     TIME(DATE_ADD(v_full_date, INTERVAL t.id SECOND)), 
     HOUR(DATE_ADD(v_full_date, INTERVAL t.id SECOND)), 
     MINUTE(DATE_ADD(v_full_date, INTERVAL t.id SECOND)), 
     SECOND(DATE_ADD(v_full_date, INTERVAL t.id SECOND)), 
     DATE_FORMAT(DATE_ADD(v_full_date, INTERVAL t.id SECOND), '%p')      
  FROM (SELECT id FROM filler) t; 

DROP TEMPORARY TABLE filler;
END;

call timedimbuild();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文