mysql存储过程 二维数组_mysql之存储过程(二)

1、批量根据复杂的SQL查询结果插入到新表

DELIMITER &&

CREATE PROCEDURE settlePADTEST()

begin

DECLARE c_statTime VARCHAR(100);

DECLARE c_uid BIGINT(20);

DECLARE c_price DECIMAL(20,2);

DECLARE cur_award CURSOR for

SELECT sum(IFNULL(aa.`comprice`,0)+ IFNULL(aa.`chanprice`,0)) as price,aa.`distid` as userId,date_format(bb.order_time, '%Y-%m-%d %H:%i:%s') as statTime  FROM `commission` aa left join order bb on aa.order_no = bb.order_id where aa.`commission_type` =2 and aa. yn =1 and bb.order_time >="2019-01-01 00:00:00" and bb.order_time <="2019-01-31 23:59:59"  group by aa.`distid`,date_format(bb.order_time, '%Y%m%d');

OPEN cur_award;

LOOP

FETCH cur_award INTO c_price,c_uid,c_statTime;

INSERT INTO platxxxxd (aid,piid, award_user_real_name, award_user_mobile, creator_nick, creator_id, modified, yn, award_user_id, award_amount,created) VALUES (1001, 2342,'' , '', 'lalala',115, now(), 1,c_uid, c_price, c_statTime);

END LOOP;

end&&

delimiter;

2、批量根据复杂的SQL查询结果修改或插入到新表

DELIMITER &&

CREATE PROCEDURE settleTESTV2()

begin

DECLARE c_id BIGINT(20);

DECLARE c_aid BIGINT(20);

DECLARE c_pid BIGINT(20);

DECLARE c_uid BIGINT(20);

DECLARE c_price DECIMAL(20,2);

DECLARE cur_award CURSOR for

SELECT id,aid,pid,user_id,amount FROM `plad` where `act_id`=9 and `created` < '2019-11-01 00:00:00' and yn =1 and state=3;

OPEN cur_award;

LOOP

FETCH cur_award INTO c_id,c_aid,c_pid,c_uid ,c_price;

INSERT into srd  VALUES (null,c_pid,c_uid,c_id,c_price,109,83,CONCAT('lalala',c_aid),now(),now(),1,1);

set @count=(select count(1) from saat where user_id=c_uid and ynm = 1 and ure=1);

SELECT @count ;

if @count >0 then

update  saa set  atb= atb + c_price,.......................   where  user_id=c_uid and yn = 1 and ure=1 ;

else

INSERT INTO saa VALUES (null, c_pid, c_uid, c_price, c_price, 01, 1, NOW(), NOW(), 21, 14);

END IF;

END LOOP;

end&&

delimiter;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值