• MySQL 游标使用 多字段


    CREATE DEFINER=`root`@`localhost` FUNCTION `FUN_FIX_RECEIPT_CONTENT_PAYMENT`(accountStartDay varchar(10), accountEndDay varchar(10)) RETURNS int(11)
    BEGIN
            #解析原始小票前 删除解析小票
            #shopsId 商家ID
            #accountDay 10位日期 2018-01-09
            -- 定义一个或者多个 变量来接收 游标查询的列值
            DECLARE receiptContentId INT;  
            DECLARE payFlag VARCHAR(30);
            DECLARE cashAmount DOUBLE;
            DECLARE cardAmount DOUBLE;
            DECLARE scoreAmount DOUBLE;
            DECLARE thirdAmount DOUBLE;
            DECLARE alpayAmount DOUBLE;
            DECLARE wechatAmount DOUBLE;
            DECLARE otherAmount DOUBLE;
            
            
            -- 遍历数据结束标志
            DECLARE done INT DEFAULT FALSE;
            -- 游标内容
            DECLARE cursor_receipt_content CURSOR FOR select id, pay_flag, cash_price, card_price, score_price, third_party_price, alipay_price, wechat_price, other_price from t_shopping_receipt_content where 
                grasping_time >= STR_TO_DATE(CONCAT(accountStartDay,' 00:00:00'), '%Y-%m-%d %H:%i:%s')
                and grasping_time <= STR_TO_DATE(CONCAT(accountEndDay,' 23:59:59'), '%Y-%m-%d %H:%i:%s');
        -- 将结束标志绑定到游标
            DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;  
            
            -- 打开游标
            OPEN cursor_receipt_content;   
        r_loop: LOOP  
                        -- 提取游标里的数据,这里只有一个,多个用","逗号隔开
                        FETCH cursor_receipt_content INTO receiptContentId, payFlag, cashAmount, cardAmount, scoreAmount, thirdAmount, alpayAmount, wechatAmount, otherAmount;
                        IF done THEN  
                                LEAVE r_loop; 
                        END IF;
                        
                        -- 这里做你想做的循环的事件
                        if locate('现金',payFlag) > 0 then
                            INSERT INTO t_receipt_content_payment(receipt_content_id, payment_category_id, amount) VALUES(receiptContentId, 1 , cashAmount);
                        end if;
                        
                        if locate('刷卡',payFlag) > 0 then
                            INSERT INTO t_receipt_content_payment(receipt_content_id, payment_category_id, amount) VALUES(receiptContentId, 2 , cardAmount);
                        end if;
                        
                        if locate('积分',payFlag) > 0 then
                            INSERT INTO t_receipt_content_payment(receipt_content_id, payment_category_id, amount) VALUES(receiptContentId, 3 , scoreAmount);
                        end if;
                        
                        if locate('第三方',payFlag) > 0 then
                            INSERT INTO t_receipt_content_payment(receipt_content_id, payment_category_id, amount) VALUES(receiptContentId, 4 , thirdAmount);
                        end if;
                        
                        if locate('支付宝',payFlag) > 0 then
                            INSERT INTO t_receipt_content_payment(receipt_content_id, payment_category_id, amount) VALUES(receiptContentId, 5 , alpayAmount);
                        end if;
                        
                        if locate('微信',payFlag) > 0 then
                            INSERT INTO t_receipt_content_payment(receipt_content_id, payment_category_id, amount) VALUES(receiptContentId, 6 , wechatAmount);
                        end if;
                        
                        if locate('其他',payFlag) > 0 then
                            INSERT INTO t_receipt_content_payment(receipt_content_id, payment_category_id, amount) VALUES(receiptContentId, 7 , otherAmount);
                        end if;
        END LOOP r_loop;
            -- 关闭游标
        CLOSE cursor_receipt_content; 
        RETURN 0;
    END
  • 相关阅读:
    java-并发解决方案
    linux-常用快捷键
    java-进程
    vim-常用快捷键
    数据库-事务
    spring-事务实现原理
    gdb
    x86 下的 struct 變數 member 擺放位置
    du 查看 資料夾 佔用空間
    ADC 計算時,階數的選擇
  • 原文地址:https://www.cnblogs.com/eason-d/p/10811606.html
Copyright © 2020-2023  润新知