zoukankan      html  css  js  c++  java
  • MySQL 存储过程,游标,临时表创建

    -- --------------------------------------------------------------------------------
    -- Routine DDL
    -- Note: comments before and after the routine body will not be stored by the server
    -- --------------------------------------------------------------------------------
    DELIMITER $$
    
    CREATE DEFINER=`root`@`localhost` PROCEDURE `filter_record_time`(startTime timestamp, endTime timestamp, pointIndex int)
    BEGIN  
            declare _recordtime timestamp;
            declare _pointIndex smallint;
            declare _value double;
            declare _result text;
            declare _previoustime timestamp;
            DECLARE done INT DEFAULT FALSE;
            
    
            declare fetchSeqCursor cursor for select distinct RecordTime,PointIndex,Value
                                                from flow_record 
                                               where recordtime >= startTime 
                                                 and recordtime < endTime
                                                 and pointIndex = pointIndex 
                                                 and recordtime is not null
                                            order by recordtime;
            
            DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
    
            create table if not exists filter_record_time_temp(timeFrom timestamp, timeTo timestamp, times int);
        
            open fetchSeqCursor;
            
            
            seq_loop:loop
                fetch fetchSeqCursor into _recordtime, _pointIndex, _value;
    
                IF done THEN
                    LEAVE seq_loop;
                END IF;
    
                if _previoustime is null then
                    if _recordtime <> startTime then
                        insert into filter_record_time_temp select startTime timefrom, _recordtime timeto, 
                                cast((unix_timestamp(_recordtime) - unix_timestamp(startTime) - 120) / 120 as signed)  times;
                    end if;
                else 
                    if unix_timestamp(_previoustime) + 120 < unix_timestamp(_recordtime) then
                        insert into filter_record_time_temp select _previoustime timefrom, _recordtime timeto,
                                cast((unix_timestamp(_recordtime) - unix_timestamp(_previoustime) - 120) / 120 as signed) times;
                    end if;
                end if;
    
                set _previoustime = _recordtime;
            end loop;
    
            close fetchSeqCursor; 
    
            if unix_timestamp(_previoustime) + 120 < unix_timestamp(endTime) then
                insert into filter_record_time_temp select _previoustime timefrom, endTime timeto,
                                cast((unix_timestamp(endTime) - unix_timestamp(_previoustime) - 120) / 120 as signed) times;
            end if;
    
            select * from filter_record_time_temp;
    
            drop table if exists filter_record_time_temp;
    
    END

    真尼玛烦人,各个数据库sql语法都不一致,写一点东西查半天资料,耽误时间.

  • 相关阅读:
    51nod 237 最大公约数之和 V3 杜教筛
    luogu P4213 【模板】杜教筛(Sum)
    BZOJ 3527: [Zjoi2014]力 FFT
    凸多边形 HRBUST
    luogu P1354 房间最短路问题 计算几何_Floyd_线段交
    几何基础
    BZOJ 1862: [Zjoi2006]GameZ游戏排名系统 Hash + Splay
    BZOJ3529: [Sdoi2014]数表 莫比乌斯反演_树状数组
    BZOJ 2820: YY的GCD 莫比乌斯反演 + 数学推导 + 线性筛
    迭代器,三元表达式,列表生成式,字典生成式,生成器,递归(没深入理解)
  • 原文地址:https://www.cnblogs.com/sskset/p/3644710.html
Copyright © 2011-2022 走看看