原文地址: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. create pdf file using Spire.Pdf or iTextSharp or PdfSharp

    Spire.Pdf: 注:pdf 显示中文一定要设置相应的中文字体,其他外文类似.否则显示为乱码( 如果繁体的服务器上生成的中文内容PDF文档,在简体操作系统保存或并传给简体系统上查看,会存在乱码问题 ...

  2. csharp: sum columns or rows in a dataTable

    DataTable dt = setData(); // Sum rows. //foreach (DataRow row in dt.Rows) //{ // int rowTotal = 0; / ...

  3. 集合类--最详细的面试宝典--看这篇就够用了(java 1.8)

    看了一个星期源码,搜索上百篇博文,终于总结出了集合类的所有基础知识点,学集合,看这篇就够用了!!! 篇幅有点长, 如果你能全部理解,java最重要的集合就不怕了,秒过面试!!!(本篇素材来自网络,如有 ...

  4. npm 全局执行 update 、 outdated 出现 npm-debug.log 404 错误的问题

    想要执行一次全局更新,发现屡次报错: # npm update -g 提示的错误信息包含如下内容: npm ERR! code E404 npm ERR! 404 Registry returned ...

  5. 前端面试整理——javascript算法和测试题

    (1)算法: 1.斐波那契数列:1.1.2.3.5.8.13.21.输入n,输出数列中第n位数的值. 方案一: function fn(n){ var num1 = 1, num2= 1, num3 ...

  6. React 入门学习笔记整理(二)—— JSX简介与语法

    先看下这段代码: import React from 'react'; //最终渲染需要调用ReactDOM库,将jsx渲染都页面中 import ReactDOM from 'react-dom'; ...

  7. 安卓界面之Viewpager和Tablayout实现滑动界面

    摘要:六部实现选项卡界面 一. 在gradle文件添加以下代码: implementation 'com.android.support:design:28.0.0' 在gradle文件添加以上代码后 ...

  8. c#中ofType的用法

    原文:http://www.cnblogs.com/Janzen/p/5128749.html 该关键字主要用在非泛型到泛型之间的转化,在有些场合还是很有用的:比如:在使用非泛型的时候,想使用LINQ ...

  9. .net core xss攻击防御

    XSS攻击全称跨站脚本攻击,是为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS,XSS是一种在web应用中的计算机安全漏洞,它允许恶意 ...

  10. [MapReduce_4] MapTask 并发数的决定机制

    0. 说明 介绍 && Map 个数 & Reduce 个数指定 && Map 切片计算 1. 介绍 一个 job 的 Map 阶段并行度由客户端在提交 job ...