原文地址: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存储过程语法例子的更多相关文章

  1. mysql存储过程语法及实例

    存储过程如同一门程序设计语言,同样包含了数据类型.流程控制.输入和输出和它自己的函数库. --------------------基本语法-------------------- 一.创建存储过程cr ...

  2. Mysql存储过程语法

    一口气弄完了! 一.条件语句if-then-else: create procedure demo_1(in param int) begin declare var int; ; then inse ...

  3. 『007』MySQL

    『005』索引-Database MySQL [001]- 点我快速打开文章[第一章 MySQL 大纲介绍] [002]- 点我快速打开文章[第二章 MySQL 介绍和安装] 更新中

  4. mysql存储过程简单例子

    1.之前经常在oracle数据库中使用存储过程,换到mysql后用的不多,但是有时候也用,大致记录一下,基本和oracle的一样. CREATE DEFINER = `root`@`%` PROCED ...

  5. Mysql 存储过程小例子

    创建存储过程: DELIMITER $$ USE `database_name`$$ DROP PROCEDURE IF EXISTS `add_or_update_user`$$ )) BEGIN ...

  6. 重新学习MySQL数据库2:『浅入浅出』MySQL 和 InnoDB

    重新学习Mysql数据库2:『浅入浅出』MySQL 和 InnoDB 作为一名开发人员,在日常的工作中会难以避免地接触到数据库,无论是基于文件的 sqlite 还是工程上使用非常广泛的 MySQL.P ...

  7. 『浅入浅出』MySQL 和 InnoDB

    作为一名开发人员,在日常的工作中会难以避免地接触到数据库,无论是基于文件的 sqlite 还是工程上使用非常广泛的 MySQL.PostgreSQL,但是一直以来也没有对数据库有一个非常清晰并且成体系 ...

  8. Mysql 存储过程基本语法

    delimiter //一般情况下MYSQL以:结尾表示确认输入并执行语句,但在存储过程中:不是表示结束,因此可以用该命令将:号改为//表示确认输入并执行. 一.创建存储过程 1.基本语法: crea ...

  9. mysql 调用存储过程及例子

    存储过程如同一门程序设计语言,同样包含了数据类型.流程控制.输入和输出和它自己的函数库. --------------------基本语法-------------------- 一.创建存储过程 c ...

随机推荐

  1. 【读书笔记】iOS-Apple的移动设备硬件

    本书中有一个关键观点是:“硬件并不是特别重要,用户体验才是真正的杀手级应用.“尽管如此,多了解一些你使用的硬件的相关知识,对于整个项目来说是必备的,而对于设计和开发高质量的作品来说敢是不可或缺的. 人 ...

  2. python之socketserver ftp功能简单讲解

    TCP协议中的socket一次只能和一个客户端通信,然而socketserver可以实现和多个客户端通信. 它是在socket的基础上进行了一层封装,底层还是调用的socket # 服务端 impor ...

  3. TFS 安装遇到的问题

    居然是是微信桌面客户端占用了8080端口,也是醉了... 1 VS链接 源码管理器 发现提示 Http 404, 发现原来是自己吧tfs 给删除了 2 重新安装tfs,过程中提示 8080 端口被占用 ...

  4. Java字符串占位符(commons-text)替换(转载)

    Java字符串占位符(commons-text)替换 https://blog.csdn.net/varyall/article/details/83651798 <dependency> ...

  5. Flume Source 实例

    Flume Source 实例 Avro Source 监听avro端口,接收外部avro客户端数据流.跟前面的agent的Avro Sink可以组成多层拓扑结构. 1 2 3 4 5 6 7 8 9 ...

  6. 用PRODUCT_COPY_FILES拷贝文件夹

    拷贝文件PRODUCT_COPY_FILES += device/qcom/msm8909/media/media_profiles_8909.xml:system/etc/media_profile ...

  7. Git的安装配置(win环境)

    安装: 首先安装win版本的git msysgit:https://git-for-windows.github.io 注:安装时要勾选生成桌面快捷方式. 默认安装完后依次执行: $ git conf ...

  8. python第十七天---时间模块、random模块

    作完一个作业,开始新的学习: 有由今天的时间有限所有学习了以下两个模块,明天继续! 时间模块.random模块 import time #!usr/bin/env python #-*-coding: ...

  9. php解决前后端验证字符串长度不一致

    前端代码 function getStrleng(str){ var myLen =0; for(var i=0;i<str.length;i++){ if(str.charCodeAt(i)& ...

  10. 服务器较稳妥的磁盘阵列方案:RAID5+热备盘

    最近公司这边要整个数据中心,但是我们这边磁盘阵列的方案选择方面需要做好万全的测试 内部测试的服务器当初损坏的磁盘1个做的raid5直接换个新磁盘做替换rebuild就行了 但是现在想的方法是5+1的方 ...