MySQL过程和游标
BEGIN
DECLARE f_leastCount INT DEFAULT 100;
DECLARE f_ratio FLOAT DEFAULT 0.8; DECLARE i_channel VARCHAR(64);
DECLARE i_appVersionName VARCHAR(64);
DECLARE i_statDate DATE;
DECLARE i_accumulateCount INT;
DECLARE i_activateCount INT;
DECLARE i_dau30Count INT;
DECLARE i_dau7Count INT;
DECLARE i_dauCount INT;
DECLARE i_dauImeiCount INT;
DECLARE i_dauImsiCount INT;
DECLARE i_launchCount INT;
DECLARE i_lostCount INT;
DECLARE i_newCount INT; #遍历数据结束标志
DECLARE done INT DEFAULT FALSE; #游标
DECLARE cur CURSOR FOR SELECT ds.channel,ds.appVersionName,ds.statDate,ds.accumulateCount,ds.activateCount,ds.dau30Count,ds.dau7Count,ds.dauCount,ds.dauImeiCount,ds.dauImsiCount,ds.launchCount,ds.lostCount,ds.newCount FROM appstore_dau_stat ds WHERE ds.statDate = DATE_SUB(CURDATE(),INTERVAL m_daysdelay DAY);
#将结束标志绑定到游标
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; -- 打开游标
OPEN cur;
-- 开始循环
read_loop: LOOP
-- 提取游标里的数据
FETCH cur INTO i_channel, i_appVersionName, i_statDate, i_accumulateCount, i_activateCount, i_dau30Count, i_dau7Count, i_dauCount, i_dauImeiCount, i_dauImsiCount, i_launchCount, i_lostCount, i_newCount;
-- 声明结束的时候
IF done THEN
LEAVE read_loop;
END IF;
-- 事务处理
IF i_accumulateCount > f_leastCount THEN
SET i_accumulateCount = ROUND(i_accumulateCount * f_ratio);
END IF;
IF i_activateCount > f_leastCount THEN
SET i_activateCount = ROUND(i_activateCount * f_ratio);
END IF;
IF i_dau30Count > f_leastCount THEN
SET i_dau30Count = ROUND(i_dau30Count * f_ratio);
END IF;
IF i_dau7Count > f_leastCount THEN
SET i_dau7Count = ROUND(i_dau7Count * f_ratio);
END IF;
IF i_dauCount > f_leastCount THEN
SET i_dauCount = ROUND(i_dauCount * f_ratio);
END IF;
IF i_dauImeiCount > f_leastCount THEN
SET i_dauImeiCount = ROUND(i_dauImeiCount * f_ratio);
END IF;
IF i_dauImsiCount > f_leastCount THEN
SET i_dauImsiCount = ROUND(i_dauImsiCount * f_ratio);
END IF;
IF i_launchCount > f_leastCount THEN
SET i_launchCount = ROUND(i_launchCount * f_ratio);
END IF;
IF i_lostCount > f_leastCount THEN
SET i_lostCount = ROUND(i_lostCount * f_ratio);
END IF;
IF i_newCount > f_leastCount THEN
SET i_newCount = ROUND(i_newCount * f_ratio);
END IF; IF NOT EXISTS (SELECT id FROM appstore_dau_stat_temp WHERE channel = i_channel AND appVersionName = i_appVersionName AND statDate = i_statDate) THEN
INSERT INTO appstore_dau_stat_temp(channel,appVersionName,statDate,accumulateCount,activateCount,dau30Count,dau7Count,dauCount,dauImeiCount,dauImsiCount,launchCount,lostCount,newCount)
VALUES(i_channel, i_appVersionName, i_statDate, i_accumulateCount, i_activateCount, i_dau30Count, i_dau7Count, i_dauCount, i_dauImeiCount, i_dauImsiCount, i_launchCount, i_lostCount, i_newCount);
END IF; END LOOP;
-- 关闭游标
CLOSE cur; END
MySQL过程和游标的更多相关文章
- mysql存储过程之游标遍历数据表
原文:mysql存储过程之游标遍历数据表 今天写一个mysql存储过程,根据自己的需求要遍历一个数据表,因为对存储过程用的不多,语法不甚熟悉,加之存储过程没有调试环境,花了不少时间才慢慢弄好,故留个痕 ...
- Mysql高级之游标
原文:Mysql高级之游标 什么是游标?select 语句也许一次性会取出来n条语句,那么游标便可以一次取出来select中的一条记录.每取出来一条,便向下移动一次!可以实现很复杂逻辑! 上面还有有一 ...
- MySQL存储过程之游标实战
MySQL存储过程之游标实战 博主日前在解决一个项目需求时,没有什么好的方法,于是就来学习存储过程了,之前也是接触过,奈何年少贪玩,竟是全部又还给了大学老师-苦不堪言呐-. 先说一下业务需求吧 ...
- MariaDB MariaDB、MySQL存储过程、游标基础应用举例说明
MariaDB.MySQL存储过程.游标基础应用举例说明 by:授客 QQ:1033553122 测试环境: MariaDB-10.0.19-centos7-x86_64 实践操作: # 创建测试数据 ...
- Python使用Mysql过程中一些错误
Python使用Mysql过程中一些错误 ssh登录远程服务器 ssh ubuntu@xxx.xxx.xx.xx 第一:ubuntu终端中登录Mysql mysql -uroot -p 然后输入密码即 ...
- win10安装mysql过程&&链接过程&&备份和导入数据&&grant命令
win10安装mysql过程&&链接过程&&备份和导入数据&&grant命令 一 .安装 一开始在mysql官网(https://www.mysql ...
- 解析MySQL存储过程的游标执行过程
GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源. 内容提纲 一.测试环境搭建 二.执行过程解析 三.注意事项 一.测试环境搭建 首先创建一张表,并插入几行数据字段: CRE ...
- MYSQL存储过程、游标、触发器
MySQL5 中添加了存储过程的支持. 大多数SQL语句都是针对一个或多个表的单条语句.并非所有的操作都怎么简单.经常会有一个完整的操作需要多条才能完成 存储过程简单来说,就是为以后的使用而保存的一 ...
- MySQL笔记 存储过程 游标 触发器
第二十三章 使用存储过程 MySQL5 中添加了存储过程的支持. 大多数SQL语句都是针对一个或多个表的单条语句.并非所有的操作都怎么简单.经常会有一个完整的操作需要多条才能完成 存储过程简单来说,就 ...
随机推荐
- 【快学springboot】9.使用 @Transactional 注解配置事务管理
介绍 springboot对数据库事务的使用非常的方便,只需要在方法上添加@Transactional注解即可.Spring 为事务管理提供了丰富的功能支持.Spring 事务管理分为编程式和声明式的 ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 按钮:按钮大小
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- java反射初探
java反射 反射是java的重要特性之一,java.lang.reflect 是jdk支持反射的重要包,我下面可能会对构造器Constructor,属性Filed,方法Method会用到.反射其实很 ...
- 【Linux】centos7下解决yum -y install mysql-server 没有可用包
第一步:安装从网上下载文件的wget命令 [root@localhost ~]# yum -y install wget 第二步:下载mysql的repo源 [root@localhost ~]# w ...
- vue 项目上传到码云,push时error: failed to push some refs to 'https://gitee.com/mawenrou/vue_ht.git'
vue 项目上传到码云,push时error: failed to push some refs to 'https://gitee.com/mawenrou/vue_ht.git' 因为之前已经创建 ...
- AJAX封装数据处理简单操作
数据的封装处理主要展现在JS中,在页面里面引入封装的JS, "js/ajax.js" 简单封装将get和post方法都写入,get的方法和post的方法依然需要严格区分,包括typ ...
- 4 (计算机网络) DHCP与PXE:IP是怎么来的,又是怎么没的?
如何配置 IP 地址? 那如何配置呢?如果有相关的知识和积累,你可以用命令行自己配置一个地址.可以使用 ifconfig,也可以使用 ip addr.设置好了以后,用这两个命令,将网卡 up 一下,就 ...
- c++中比较好用的“黑科技”
切入正题,上黑科技 一.黑科技函数(常用的我就不写了,例如sort函数) 1.next_permutation(a+1,a+1+n) a[1-n]全排列 2.reverse(a+1,a+1+n) 将a ...
- Link Analysis_1_Basic Elements
1. Edge Attributes 1.1 Methods of category 1.1.1 Basic three categories in terms of number of layers ...
- python获取最大、最小值
1.获取数组极值,并返回索引 c = [-10,-5,0,5,3,10,15,-20,25] print c.index(min(c)) # 返回最小值 print c.index(max(c)) ...