大岭山网站,如何在网络上做广告,公众号排名优化软件,资讯网站模板带会员投稿功能mysql 游标的用法和作用#xff0c;话不多说#xff0c;这个是网上看到的例子#xff0c;简答粗暴。例子#xff1a;当前有三张表a、b、c其中a和b是一对多关系#xff0c;b和c是一对多关系#xff0c;现在需要将b中a表的主键存到c中#xff1b;常规思路就是将b中查询出来…mysql 游标的用法和作用话不多说这个是网上看到的例子简答粗暴。例子当前有三张表a、b、c其中a和b是一对多关系b和c是一对多关系现在需要将b中a表的主键存到c中常规思路就是将b中查询出来然后通过一个update语句来更新c表就可以了但是b表中有2000多条数据难道要执行2000多次显然是不现实的最终找到写一个存储过程然后通过循环来更新c表然而存储过程中的写法用的就是游标的形式。简介游标实际上是一种能从包括多条数据记录的结果集中每次提取一条记录的机制。游标充当指针的作用。(有C的味道了)尽管游标能遍历结果中的所有行但他一次只指向一行。游标的作用就是用于对查询数据库所返回的记录进行遍历以便进行相应的操作。用法一、声明一个游标: declare 游标名称 CURSOR for table;(这里的table可以是你查询出来的任意集合)二、打开定义的游标:open 游标名称;三、获得下一行数据:FETCH 游标名称 into testrangeid,versionid;四、需要执行的语句(增删 改查):这里视具体情况而定五、释放游标:CLOSE 游标名称;实例-BEGIN--定义变量declare testrangeid BIGINT;declare versionid BIGINT;declare done int;--创建游标并存储数据declare cur_test CURSOR forselect id as testrangeid,version_id as versionid from tp_testrange;--游标中的内容执行完后将done设置为1DECLARE CONTINUE HANDLER FOR NOT FOUND SET done1;--打开游标open cur_test;--执行循环posLoop:LOOP--判断是否结束循环IF done1 THENLEAVE posLoop;END IF;--取游标中的值FETCH cur_test into testrangeid,versionid;--执行更新操作update tp_data_execute set version_idversionid where testrange_id testrangeid;END LOOP posLoop;--释放游标CLOSE cur_test;END-例子2--在windows系统中写存储过程时如果需要使用declare声明变量需要添加这个关键字否则会报错。delimiter //drop procedure if exists StatisticStore;CREATE PROCEDURE StatisticStore()BEGIN--创建接收游标数据的变量declare c int;declare n varchar(20);--创建总数变量declare total int default 0;--创建结束标志变量declare done int default false;--创建游标declare cur cursor for select name,count from store where name iphone;--指定游标循环结束时的返回值declare continue HANDLER for not found set done true;--设置初始值set total 0;--打开游标open cur;--开始循环游标里的数据read_loop:loop--根据游标当前指向的一条数据fetch cur into n,c;--判断游标的循环是否结束if done thenleave read_loop; --跳出游标循环end if;--获取一条数据时将count值进行累加操作这里可以做任意你想做的操作set total total c;--结束游标循环end loop;--关闭游标close cur;--输出结果select total;END;--调用存储过程call StatisticStore();fetch是获取游标当前指向的数据行并将指针指向下一行当游标已经指向最后一行时继续执行会造成游标溢出。使用loop循环游标时他本身是不会监控是否到最后一条数据了像下面代码这种写法就会造成死循环read_loop:loopfetch cur into n,c;set total totalc;end loop;在MySql中造成游标溢出时会引发mysql预定义的NOT FOUND错误所以在上面使用下面的代码指定了当引发not found错误时定义一个continue 的事件指定这个事件发生时修改done变量的值。declare continue HANDLER for not found set done true;--判断游标的循环是否结束if done thenleave read_loop; --跳出游标循环end if;如果done的值是true就结束循环。继续执行下面的代码使用方式第一种就是上面的实现使用loop循环第二种方式如下使用while循环drop procedure if exists StatisticStore1;CREATE PROCEDURE StatisticStore1()BEGINdeclare c int;declare n varchar(20);declare total int default 0;declare done int default false;declare cur cursor for select name,count from store where name iphone;declare continue HANDLER for not found set done true;set total 0;open cur;fetch cur into n,c;while(not done) doset total total c;fetch cur into n,c;end while;close cur;select total;END;call StatisticStore1();第三种方式是使用repeat执行drop procedure if exists StatisticStore2;CREATE PROCEDURE StatisticStore2()BEGINdeclare c int;declare n varchar(20);declare total int default 0;declare done int default false;declare cur cursor for select name,count from store where name iphone;declare continue HANDLER for not found set done true;set total 0;open cur;repeatfetch cur into n,c;if not done thenset total total c;end if;until done end repeat;close cur;select total;END;call StatisticStore2();游标嵌套在mysql中每个begin end 块都是一个独立的scope区域由于MySql中同一个error的事件只能定义一次如果多定义的话在编译时会提示Duplicate handler declared in the same block。drop procedure if exists StatisticStore3;CREATE PROCEDURE StatisticStore3()BEGINdeclare _n varchar(20);declare done int default false;declare cur cursor for select name from store group by name;declare continue HANDLER for not found set done true;open cur;read_loop:loopfetch cur into _n;if done thenleave read_loop;end if;begindeclare c int;declare n varchar(20);declare total int default 0;declare done int default false;declare cur cursor for select name,count from store where name iphone;declare continue HANDLER for not found set done true;set total 0;open cur;iphone_loop:loopfetch cur into n,c;if done thenleave iphone_loop;end if;set total total c;end loop;close cur;select _n,n,total;end;begindeclare c int;declare n varchar(20);declare total int default 0;declare done int default false;declare cur cursor for select name,count from store where name android;declare continue HANDLER for not found set done true;set total 0;open cur;android_loop:loopfetch cur into n,c;if done thenleave android_loop;end if;set total total c;end loop;close cur;select _n,n,total;end;beginend;end loop;close cur;END;call StatisticStore3();动态SQLMysql 支持动态SQL的功能set sqlStrselect * from table where condition1 ?;prepare s1 for sqlStr;--如果有多个参数用逗号分隔execute s1 using condition1;--手工释放或者是 connection 关闭时 server 自动回收deallocate prepare s1;