日期时间的SQLite和小数秒比较

时间:2016-03-07 18:02:48

标签: sqlite

所以,我有一个以下的SQLite查询:

    DELIMITER $$
DROP PROCEDURE IF EXISTS bump_pro
$$
CREATE PROCEDURE bump_pro(curr_hour INT, curr_minute INT)
MODIFIES SQL DATA
BEGIN
    DECLARE done INT DEFAULT 0;
    DECLARE cur_user_id VARCHAR(255);
    DECLARE search_hour VARCHAR(7);
    DECLARE search_min VARCHAR(7);
    DECLARE randBump INT;
    DECLARE newHour INT;
    DECLARE newMinute INT;

    DECLARE get_bump_id CURSOR FOR SELECT user_id FROM bump_iteration;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;


    IF (curr_hour = 0 OR curr_hour = 1) THEN
        SET search_hour = 'hour1';
        SET search_min = 'min1';
    ELSEIF (curr_hour = 6 OR curr_hour = 7) THEN
        SET search_hour = 'hour2';
        SET search_min = 'min2';
    ELSEIF (curr_hour = 12 OR curr_hour = 13) THEN
        SET search_hour = 'hour3';
        SET search_min = 'min3';
    ELSEIF (curr_hour = 18 OR curr_hour = 19) THEN
        SET search_hour = 'hour4';
        SET search_min = 'min4';
    END IF;

    SELECT * FROM bump_iteration;


    SET @select  = CONCAT("CREATE TEMPORARY TABLE IF NOT EXISTS bump_iteration AS (SELECT * FROM bump_table WHERE ", search_hour ," = ", curr_hour ," AND ", search_min ," = ", curr_minute ,");");
    PREPARE stm FROM @select;
    EXECUTE stm;
    DEALLOCATE PREPARE stm;

    OPEN get_bump_id;
    addLoop: LOOP
        FETCH get_bump_id INTO cur_user_id;
        IF (done = 1) THEN
            SELECT done as done1;
            LEAVE addLoop;
        END IF;
        /* RANDOM NUMBER BETWEEN 2 HOURS */
        SELECT ROUND((RAND() * (119-0))+0) INTO randBump;
        IF (curr_hour = 0 OR curr_hour = 1) THEN
            IF (randBump >= 60) THEN
                SET newHour = 1;
                SET newMinute = randBump - 60;
            ELSEIF (randBump < 60) THEN
                SET newHour = 0;
                SET newMinute = 60 - randBump;
            END IF;
        ELSEIF (curr_hour = 6 OR curr_hour = 7) THEN
            IF (randBump >= 60) THEN
                SET newHour = 7;
                SET newMinute = randBump - 60;
            ELSEIF (randBump < 60) THEN
                SET newHour = 6;
                SET newMinute = 60 - randBump;
            END IF;
        ELSEIF (curr_hour = 12 OR curr_hour = 13) THEN
            IF (randBump >= 60) THEN
                SET newHour = 13;
                SET newMinute = randBump - 60;
            ELSEIF (randBump < 60) THEN
                SET newHour = 12;
                SET newMinute = 60 - randBump;
            END IF;
        ELSEIF (curr_hour = 18 OR curr_hour = 19) THEN
            IF (randBump >= 60) THEN
                SET newHour = 19;
                SET newMinute = randBump - 60;
            ELSEIF (randBump < 60) THEN
                SET newHour = 18;
                SET newMinute = 60 - randBump;
            END IF;
        END IF;
        SELECT cur_user_id;
        UPDATE bump_table SET hour1 = 1, min1 = 13, bump = 5;
/*
        SET @update  = CONCAT("UPDATE bump_table SET ", search_hour ," = ", newHour ,", ", search_min ," = ", newMinute ,", bump = 4 WHERE user_id = cur_user_id;");
        PREPARE stm FROM @update;
        EXECUTE stm;
        DEALLOCATE PREPARE stm;
*/

    END LOOP addLoop;
    CLOSE get_bump_id;
END;
$$

令我惊讶的是,我得到的结果是...... 1(真实)。无论我在点之后放置什么值,当小数值不同时总是如此。

According to docs

  

在格式4,7和10中,小数秒值SS.SSS可以在小数点后面有一个或多个数字。示例中显示了正好三位数,因为只有前三位数对结果有意义,但输入字符串可以有少于或多于三位数,日期/时间函数仍然可以正常运行。

是否存在某种错误,或者是否有某些我不理解的内容?

1 个答案:

答案 0 :(得分:0)

您链接的页面显示:

  

datetime()函数返回&#34; YYYY-MM-DD HH:MM:SS&#34;。

如果您知道您的值已经完全采用正确的格式,请直接比较字符串。否则,将datetime()替换为带有mmap的strftime(),将值转换为您想要的格式。