Time dimension fill problem

柔情痞子 提交于 2020-01-03 04:58:29

问题


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');

回答1:


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




回答2:


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();


来源:https://stackoverflow.com/questions/6413175/time-dimension-fill-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!