『转』MySQL存储过程语法例子
原文地址:http://blog.chinaunix.net/uid-540802-id-138873.html
------------------------- 自动生成随机数据存储过程 -------------------------
drop PROCEDURE if exists genRand;
delimiter //
create PROCEDURE genRand(in rank int, in add_num int, in statTime char(10))
-- rank:随机最大邮件数, add_num:生成条数, startTime:统计数据日期
Begin
declare mobile long;
declare tmp int;
set mobile = 13600000000;
set tmp = 1;
-- delete from ippush_stats_email;
while tmp <= add_num do
insert into ippush_stats_email values(mobile + tmp, 1, now(), statTime,
random_int(rank), random_int(rank), random_int(rank), random_int(rank),random_int(rank),
random_int(rank), random_int(rank), random_int(rank), random_int(rank),random_int(rank),
random_int(rank), random_int(rank), random_int(rank), random_int(rank),random_int(rank),
random_int(rank), random_int(rank), random_int(rank), random_int(rank),random_int(rank),
random_int(rank), random_int(rank), random_int(rank), random_int(rank) );
set tmp = tmp + 1;
end while;
end//
delimiter ;
call genRand(10, 10, '2010-10-6');
------------------------- 统计从time1到time2时段的 -------------------------
drop PROCEDURE if exists doStats;
delimiter //
create PROCEDURE doStats(in time1 char(10), in time2 char(10))
Begin
declare _mobile char(11);
declare _sums int;
declare _stat_time date;
declare _count int;
declare _user_type int;
declare fetchSeqOk int;
declare fetchSeqCursor cursor for
select mobile, count(*) as count, user_type, stat_time, max((clock_1 + clock_2 + clock_3+clock_4+clock_5+clock_6+clock_7+clock_8+clock_9+
clock_10+clock_11+clock_12+clock_13+clock_14+clock_15+clock_16+clock_17+clock_18+clock_19+
clock_20+clock_21+clock_22+clock_23+clock_24)) as sums
from ippush_stats_email
where stat_time >= time1 and stat_time <= time2
group by mobile;
declare CONTINUE HANDLER FOR NOT FOUND SET fetchSeqOk = 0;
set fetchSeqOk = 1;
open fetchSeqCursor;
while fetchSeqOk = 1 do
fetch fetchSeqCursor into _mobile, _count, _user_type, _stat_time, _sums;
if _sums > 30 then
call onelineStat(1, _user_type, time1, time2, _mobile);
else
call onelineStat(2, _user_type, time1, time2, _mobile);
end if;
end while;
close fetchSeqCursor;
end//
delimiter ;
call doStats('2010-10-01', '2010-11-05');
------------------------- 一条业务线的统计 --------------------------
drop PROCEDURE if exists onelineStat;
delimiter //
create PROCEDURE onelineStat(in type int, in inuser_type int, in time1 char(10), in time2 char(10), in inmobile char(11))
Begin
if inuser_type = 1 then -- is mas user
if type = 1 then -- is level 1
insert into ippush_strategy (select mobile, inuser_type, concat(max(clock_1)>=4,
max(clock_2)>=4,
max(clock_3)>=4,
max(clock_4)>=4,
max(clock_5)>=4,
max(clock_6)>=4,
max(clock_7)>=4,
max(clock_8)>=4,
'111111111',
max(clock_19)>=4,
max(clock_20)>=4,
max(clock_21)>=4,
max(clock_22)>=4,
max(clock_23)>=4,
max(clock_24)>=4), now(), time1, time2
from ippush_stats_email
where mobile = inmobile and stat_time >= time1 and stat_time <= time2 and user_type = inuser_type
group by mobile ) ;
else
insert into ippush_strategy ( select mobile, inuser_type, concat(max(clock_1)>=4,
max(clock_2)>=4,
max(clock_3)>=4,
max(clock_4)>=4,
max(clock_5)>=4,
max(clock_6)>=4,
max(clock_7)>=4,
max(clock_8)>=4,
max(clock_9)>=4,
max(clock_10)>=4,
max(clock_11)>=4,
max(clock_12)>=4,
max(clock_13)>=4,
max(clock_14)>=4,
max(clock_15)>=4,
max(clock_16)>=4,
max(clock_17)>=4,
max(clock_18)>=4,
max(clock_19)>=4,
max(clock_20)>=4,
max(clock_21)>=4,
max(clock_22)>=4,
max(clock_23)>=4,
max(clock_24)>=4), now(), time1, time2
from ippush_stats_email
where mobile = inmobile and stat_time >= time1 and stat_time <= time2 and user_type = inuser_type
group by mobile ) ;
end if;
else -- is mig user
if type = 1 then
insert into ippush_strategy values(inmobile, inuser_type, '111111111111111111111111', now, time1, time2);
end if;
end if;
end//
delimiter ;
------------------ 调用 --------------------
call onelineStat(1, 1, '2010-10-01', '2010-11-05', '13600000001');
『转』MySQL存储过程语法例子的更多相关文章
- mysql存储过程语法及实例
存储过程如同一门程序设计语言,同样包含了数据类型.流程控制.输入和输出和它自己的函数库. --------------------基本语法-------------------- 一.创建存储过程cr ...
- Mysql存储过程语法
一口气弄完了! 一.条件语句if-then-else: create procedure demo_1(in param int) begin declare var int; ; then inse ...
- 『007』MySQL
『005』索引-Database MySQL [001]- 点我快速打开文章[第一章 MySQL 大纲介绍] [002]- 点我快速打开文章[第二章 MySQL 介绍和安装] 更新中
- mysql存储过程简单例子
1.之前经常在oracle数据库中使用存储过程,换到mysql后用的不多,但是有时候也用,大致记录一下,基本和oracle的一样. CREATE DEFINER = `root`@`%` PROCED ...
- Mysql 存储过程小例子
创建存储过程: DELIMITER $$ USE `database_name`$$ DROP PROCEDURE IF EXISTS `add_or_update_user`$$ )) BEGIN ...
- 重新学习MySQL数据库2:『浅入浅出』MySQL 和 InnoDB
重新学习Mysql数据库2:『浅入浅出』MySQL 和 InnoDB 作为一名开发人员,在日常的工作中会难以避免地接触到数据库,无论是基于文件的 sqlite 还是工程上使用非常广泛的 MySQL.P ...
- 『浅入浅出』MySQL 和 InnoDB
作为一名开发人员,在日常的工作中会难以避免地接触到数据库,无论是基于文件的 sqlite 还是工程上使用非常广泛的 MySQL.PostgreSQL,但是一直以来也没有对数据库有一个非常清晰并且成体系 ...
- Mysql 存储过程基本语法
delimiter //一般情况下MYSQL以:结尾表示确认输入并执行语句,但在存储过程中:不是表示结束,因此可以用该命令将:号改为//表示确认输入并执行. 一.创建存储过程 1.基本语法: crea ...
- mysql 调用存储过程及例子
存储过程如同一门程序设计语言,同样包含了数据类型.流程控制.输入和输出和它自己的函数库. --------------------基本语法-------------------- 一.创建存储过程 c ...
随机推荐
- Vue和React的对比
今晚我们来搞一搞Vue和React的对比好吧,话不多说今天我们直接开搞可好,各位小老板,开始吧 1. react整体是函数式的思想,把组件设计成纯组件,状态和逻辑通过参数传入, 所以在react中,是 ...
- python之网络通信协议
TCP/IP五层协议和OSI的七层协议: TCP和UDP的区别: Tcp协议:面向连接,数据可靠,传输效率低,面向字节流 Udp协议:面向无连接,数据不可靠,传输效率高,面向报文
- js 监听事件的叠加和移除
html DOM元素有很多on开头的监听事件,如onload.onclick等,见DOM事件列表.但是同一种事件,后面注册的会覆盖前面的: window.onresize = function(){ ...
- Linux下修改IP、DNS、路由命令行设置
本文最后修改时间:20180313 一.快速修改,重启后设置就没了 ifconfig eth0 192.168.1.22 netmask 255.255.255.0 up route add defa ...
- python第六十一天,第六十二天 redis
redis 缓存系统 redis是业界主流的key-value nosql 数据库之一.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).se ...
- 修改Devexpress DateEdit控件默认的日期格式和日历风格
最近项目中很多地方使用到DevExpress的DateEdit控件,而且设置都是一致(日期格式.选择日期的日历的风格等).每一次都需要设置太痛苦,因此需要寻找一种高效的解决方式. 一. 设置好一个作为 ...
- json数据 二级联动
<head> <link href="static/bootstrap-3.3.5-dist/css/bootstrap.css" rel="style ...
- CentOS7搭建OpenVPN
目录 CentOS7搭建OpenVPN 环境 安装 第一步.安装openvpn及所需软件 第二步.编辑vars文件,根据自己环境配置 第三步.创建服务端证书及key 第四步.创建客户端证书 第五步.拷 ...
- 紧急整理了 20 道 Spring Boot 面试题,我经常拿来面试别人!
面试了一些人,简历上都说自己熟悉 Spring Boot, 或者说正在学习 Spring Boot,一问他们时,都只停留在简单的使用阶段,很多东西都不清楚,也让我对面试者大失所望. 下面,我给大家总结 ...
- 在VUE应用中配置ESLint(代码检查)
eslint配置方式 注释配置:使用js注释来直接嵌入ESLint配置信息到一个文件里 配置文件:使用一个js文件,JSON或者YAML文件来给整个目录和它的子目录指定配置信息.这些配置可以写在一个文 ...