博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MySQL——循环(双重循环)
阅读量:5967 次
发布时间:2019-06-19

本文共 4945 字,大约阅读时间需要 16 分钟。

-- 函数-- 设置好时区set time_zone = '+8:00';-- 开启事件调度器set GLOBAL event_scheduler = 1; drop event if exists commission_ms_ever_month;  # 设置分隔符为 '$$' DELIMITER $$ create event commission_ms_ever_month  on schedule every 1 MONTH  starts '2018-07-01 03:00:00'do begin         CALL proc_commission_ms();end $$ # 将语句分割符设置回 ';' DELIMITER ; DROP PROCEDURE IF EXISTS proc_commission_ms;CREATE PROCEDURE proc_commission_ms()  BEGIN  -- 1、下户就有提成,但订单状态为拒单或者放款时才加入计算-- 2、以填写面审信息流程的第一次提交账号为提成归属人-- 3、以主管审核(风控助理操作)流程的第一次提交时间作为下户时间;(所以要有终审节点)-- 4、下户时间与拒单时间,下户时间与放款时间不能跨3个月;-- !!! v_select_mytask_txmsxx 填写面试信息  v_select_mytask_zgsh主管审核 v_select_mytask_zs终审 v_order_reject拒单 v_task_loaned已放款        -- 定义变量 (变量名称不能和select 接收别名一样)    DECLARE txTime1 datetime;      DECLARE orderCode1 CHAR(100);      DECLARE receiveId1 int(11);      DECLARE receiveName1 CHAR(100);    DECLARE xhTime1 datetime;    DECLARE submitRole1 CHAR(255);            DECLARE orderCode2 CHAR(100);     DECLARE doneTime2 CHAR(100);    -- 定义结束标识 并绑定游标    DECLARE done INT DEFAULT FALSE;    DECLARE edone INT DEFAULT FALSE;     -- 定义游标01 --- 没有3个月限制的数据 以及结束标识    DECLARE _outerForEach CURSOR FOR              SELECT                MIN(tx.finish_time) AS txTime,                tx.order_code AS orderCode,                tx.receive_id AS receiveId,                tx.receive_name AS receiveName,                MIN(sh.finish_time) AS xhTime,                tx.submit_role AS submitRole            FROM                v_mytask_txmsxx tx,                v_mytask_zgsh sh            WHERE                tx.order_code = sh.order_code            AND tx.order_code in(SELECT order_code FROM v_mytask_zs)            AND                 tx.order_code IN (                    SELECT order_code FROM v_order_lastmonth_reject_loaned -- 上个月产生的新拒单+放款                )            GROUP BY                tx.order_code;        DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;-- 结束标识            # 打开游标1     OPEN _outerForEach;           read_loop: LOOP  -- 循环游标开始,            FETCH _outerForEach INTO txTime1, orderCode1, receiveId1, receiveName1, xhTime1, submitRole1;                        IF done THEN                            LEAVE read_loop;#跳出循环                        END IF;                         BEGIN                             # 定义游标02 --- 已放款的和拒单的单子 --并 排除已经进入面审提成库的单子                            DECLARE _innerForEach CURSOR FOR                                       SELECT order_code as orderCode,finish_time AS doneTime FROM v_order_reject                                     WHERE order_code not in(SELECT order_code FROM commission_inquiry_collection WHERE type = 1)                                     UNION ALL                                      SELECT orderCode as orderCode,finishTime AS doneTime FROM v_task_loaned                                    WHERE orderCode not in(SELECT order_code FROM commission_inquiry_collection WHERE type = 1);                               DECLARE CONTINUE HANDLER FOR NOT FOUND SET edone = 1;#结束标识                             # 打开游标2                              OPEN _innerForEach;                                 inner_loop: LOOP                                     FETCH _innerForEach INTO orderCode2,doneTime2;                                         IF edone THEN                                             LEAVE inner_loop;                                         ELSE                                        #(处理业务)订单号一致,而且时间差3个月以内,插入统计表                                              IF (orderCode2 = orderCode1 AND xhTime1 IS NOT NULL AND doneTime2 IS NOT NULL AND TIMESTAMPDIFF(MONTH,xhTime1,doneTime2) <= 3) THEN                                                  INSERT INTO `commission_inquiry_collection` (                                                     `user_id`,                                                     `order_code`,                                                     `execute_time`,                                                     `task_code`,                                                     `count_time`,                                                    `commission_time`,                                                     `type`,                                                     `submit_role`,                                                     `city`,                                                     `ext3`                                                 ) VALUES (receiveId1,orderCode1,xhTime1,"T_TXMSXX_0002",NOW(),DATE_SUB(CURDATE(),INTERVAL 1 MONTH),'1',submitRole1,NULL,NULL);                                              END IF;                                     END IF;                                 END LOOP;                             CLOSE _innerForEach;-- 关闭内层游标                            SET edone = FALSE;-- 内循环复位 以便再次循环                         END;            END LOOP; -- 结束循环    CLOSE _outerForEach;-- 关闭游标     COMMIT;END;

 

转载于:https://www.cnblogs.com/speily/p/9289113.html

你可能感兴趣的文章
SCCM TP4部署Office2013
查看>>
Linux系统启动过程,grub重装。
查看>>
使用Putty密钥认证机制远程登录Linux
查看>>
【博客话题】技术人生之三界修炼
查看>>
Ext JS 6开发实例(三) :主界面设计
查看>>
【原创】Oracle RAC原理和安装
查看>>
东哥读书小记 之 《MacTalk人生元编程》
查看>>
《随机出题软件》&《随机分队软件》源码(Windows API)
查看>>
python 文件及文件夹操作
查看>>
Android自定义ListView的Item无法响应OnItemClick的解决办法
查看>>
Building Apps for Windows Phone 8.1教程下载地址整理
查看>>
移动Web—CSS为Retina屏幕替换更高质量的图片
查看>>
[Linux 性能检测工具]SAR
查看>>
JS 运行、复制、另存为 代码。
查看>>
一个经典编程面试题的“隐退”
查看>>
阿里公共DNS 正式发布了
查看>>
Java抓取网页数据(原网页+Javascript返回数据)
查看>>
EasyUI基础入门之Pagination(分页)
查看>>
ORACLE中CONSTRAINT的四对属性
查看>>
python 迭代器 生成器
查看>>