mysql使用游标循环结果集

存储过程:

CREATE DEFINER=`orion`@`%` PROCEDURE `task`()
BEGIN
		#定义需要操作结果集中的变量
		declare id int(11);
		declare name varchar(50);
		#设置开始标志,默认是true,如果为false循环结束
		declare flag int default true;
		#将结果集扔进定义的游标中
		

    declare yb cursor for select chapter_id,if(chapter_name REGEXP '^[0-9]',REPLACE(REPLACE(SUBSTRING_INDEX(chapter_name,' ',-1), CHAR(10),''), CHAR(13),''),chapter_name) as tmp from sc_title_chapter where material_id=123398 and grade_id in(158737,158738,158739,158740,158741,158742) and subject_id='s101'and chapter_name not  REGEXP '单元|综合|习作|语文|口语|读书|诗|文言文';
    		#声明如果flag为false时循环结束
    		declare continue handler for not found set flag = false;
    		#开是循环游标
    		open yb;
    		#具体操作
    		repeat
    					fetch yb into id,name;
    					set @chapter_id=(select chapter_id from sc_title_chapter where chapter_name REGEXP name and total!=0 order by total desc limit 1);
    					#if(@chapter_id!="")
    					insert into tmp values(@chapter_id,id,name);
    					#end if;
    		#循环结束
    		until flag = false
    		end repeat;
    		close yb;
    END

执行语句

delete from tmp;
call task;

记录结束!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值