1. -- --------------------------------------------------------------------------------
  2. -- Routine DDL
  3. -- Note: comments before and after the routine body will not be stored by the server
  4. -- --------------------------------------------------------------------------------
  5. DELIMITER $$
  6.  
  7. CREATE DEFINER=`root`@`localhost` PROCEDURE `filter_record_time`(startTime timestamp, endTime timestamp, pointIndex int)
  8. BEGIN
  9. declare _recordtime timestamp;
  10. declare _pointIndex smallint;
  11. declare _value double;
  12. declare _result text;
  13. declare _previoustime timestamp;
  14. DECLARE done INT DEFAULT FALSE;
  15.  
  16. declare fetchSeqCursor cursor for select distinct RecordTime,PointIndex,Value
  17. from flow_record
  18. where recordtime >= startTime
  19. and recordtime < endTime
  20. and pointIndex = pointIndex
  21. and recordtime is not null
  22. order by recordtime;
  23.  
  24. DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
  25.  
  26. create table if not exists filter_record_time_temp(timeFrom timestamp, timeTo timestamp, times int);
  27.  
  28. open fetchSeqCursor;
  29.  
  30. seq_loop:loop
  31. fetch fetchSeqCursor into _recordtime, _pointIndex, _value;
  32.  
  33. IF done THEN
  34. LEAVE seq_loop;
  35. END IF;
  36.  
  37. if _previoustime is null then
  38. if _recordtime <> startTime then
  39. insert into filter_record_time_temp select startTime timefrom, _recordtime timeto,
  40. cast((unix_timestamp(_recordtime) - unix_timestamp(startTime) - 120) / 120 as signed) times;
  41. end if;
  42. else
  43. if unix_timestamp(_previoustime) + 120 < unix_timestamp(_recordtime) then
  44. insert into filter_record_time_temp select _previoustime timefrom, _recordtime timeto,
  45. cast((unix_timestamp(_recordtime) - unix_timestamp(_previoustime) - 120) / 120 as signed) times;
  46. end if;
  47. end if;
  48.  
  49. set _previoustime = _recordtime;
  50. end loop;
  51.  
  52. close fetchSeqCursor;
  53.  
  54. if unix_timestamp(_previoustime) + 120 < unix_timestamp(endTime) then
  55. insert into filter_record_time_temp select _previoustime timefrom, endTime timeto,
  56. cast((unix_timestamp(endTime) - unix_timestamp(_previoustime) - 120) / 120 as signed) times;
  57. end if;
  58.  
  59. select * from filter_record_time_temp;
  60.  
  61. drop table if exists filter_record_time_temp;
  62.  
  63. END

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

MySQL 存储过程,游标,临时表创建的更多相关文章

  1. MySQL存储过程 游标

    MySQL存储过程  游标 如何在存储过程中使用MySQL游标来遍历SELECT语句返回的结果集 MySQL游标简介 要处理存储过程中的结果集,请使用游标.游标允许您迭代查询返回的一组行,并相应地处理 ...

  2. mysql存储过程游标嵌套循环

    自己写的一个mysql存储过程如下: BEGIN DECLARE _did bigint(20);DECLARE _count int;DECLARE s1 int;DECLARE cur_1 CUR ...

  3. mysql 存储过程、视图---创建、调用、删除

    之前一直用的是Sql Server数据库,最近偶然机会接触到mysql.这里总结了关于mysql 存储过程.视图的“创建.调用.删除”示例 ============================== ...

  4. mysql存储过程游标加计划任务事件调度器

    存储过程加事件调度器 -- 存储过程 (多个)游标的使用 临时表的使用(让执行时间从一个小时降低到5分钟)DELIMITER $$ DROP PROCEDURE IF EXISTS `eval_cal ...

  5. 菜鸟使用MySQL存储过程and临时表,供新手参考,请高手斧正

    因为公司最近的一个项目,第一次用到了MySQL(5.10版本),之前听传说MySQL很厉害的样子,因为开源而神奇,但是现在用起来, 感觉并不好啊!我知道是我水平太down,呜呜呜,请各路神仙略施小技, ...

  6. MySQL 存储过程/游标/事务

    将会用到的几个表 mysql> DESC products; +------------+--------------+------+-----+---------+-------------- ...

  7. MySQL存储过程和临时表

    MySQL创建存储过程 MySQL中,创建存储过程的基本形式如下: CREATE PROCEDURE sp_name ([proc_parameter[,...]]) [characteristic ...

  8. php调用mysql存储过程游标

    <?php $dbtype = 'mysql'; $host = 'localhost'; $dbname = 'test'; $dsn = "$dbtype:host=$host;d ...

  9. MySQL 存储过程游标

    一.创建游标 游标用declare语句创建.如下面的例子所示: create procedure test2() begin declare cursorTest cursor for select ...

随机推荐

  1. SEM关键词的三种分类方式

    关键词分类是为了使sem账户搭建结构清晰便于管理关键词.基于对需求人群的深入分析,每个账户都有其独特的分类方式,比如招商加盟行业更多的是地域分类,品牌类企业通常用词性分类即可,而冷门行业用人群分类比较 ...

  2. Permission denied: mod_fcgid

    [Tue Jun 16 13:29:08 2015] [warn] (13)Permission denied: mod_fcgid: spawn process /var/www/cgi-bin/g ...

  3. 【Semantic segmentation】Fully Convolutional Networks for Semantic Segmentation 论文解析

    目录 0. 论文链接 1. 概述 2. Adapting classifiers for dense prediction 3. upsampling 3.1 Shift-and-stitch 3.2 ...

  4. STL的其他用法(adjacent_find, find_first_of, sort_heap, merge, binary_search)总结

    2017-08-20 17:26:07 writer:pprp 1.adjacent_find() 下面是源码实现: template <class ForwardIterator> Fo ...

  5. Spring Cloud 坑点

    1 配置中心 1.config 默认Git加载 通过spring.cloud.config.server.git.uri指定配置信息存储的git地址,比如:https://github.com/spr ...

  6. 使用Github上传本地代码

    最近在学习Python,但是每次写完代码后不知道该怎么跟家里的电脑进行同步.于是开始了学习github ,方法很简单 1:注册个git账号:https://github.com 2:本地安装git软件 ...

  7. Eclipse 使用中遇到的一些问题!

    解决办法~ 1.先检查本地svn 版本与Eclipse 中svn插件 的区别 2.发现版本一致,没解决,发现如图 发现   svn接口报错 javaHL(JNI) Not Available!@ 所以 ...

  8. MySQL安装的N种方式

    一.二进制包安装 1.)下载:在官网的下载页面下的服务器操作系统选择  Linux- Generic : 进制分发版的格式是:mysql-<版本>-<OS>-tar.gz 2. ...

  9. 前端构建大法 Gulp 系列

    参考: 前端构建大法 Gulp 系列 (一):为什么需要前端构建 前端构建大法 Gulp 系列 (二):为什么选择gulp 前端构建大法 Gulp 系列 (三):gulp的4个API 让你成为gulp ...

  10. Linux查看和剔除当前登录用户

    Linux查看和剔除当前登录用户 如何在linux下查看当前登录的用户,并且踢掉你认为应该踢掉的用户? 看了网络中的一些例子.在这里总结一下.主要用到的命令有,w,who,ps,kill,pkill ...