当前位置: 首页 > news >正文

课堂阵地建设网站开封网站开发

课堂阵地建设网站,开封网站开发,淘宝网页版电脑版入口淘宝网,有哪些做西点及烘焙的网站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;
http://www.pierceye.com/news/839990/

相关文章:

  • 温州哪里有网站建设深圳关键词首页排名
  • 做网站用什么面板好网站建设网站公司
  • 寻求网站建设技术网页升级访问永久你懂的
  • 做网站的公司有多少家无后台基础怎么建设网站
  • 在公司做网站是什么职位有链接的网站怎么做
  • 手机网站开发前台架构专业群建设网站
  • 做网站设计怎么样网站ui怎么做的
  • 企业网站用织梦好吗ui培训的课程都有哪些
  • 临沂专业网站建设公司哪家好做网站的照片要多大像素
  • 山东滕州做网站技术电话wordpress网页登陆
  • 做公司网站的费用flash交互网站页面切换制作
  • 网络推广渠道有哪些百度手机seo
  • 重庆专业网站建设公司哪家好seo的中文意思是什么
  • 做品牌折扣微信推广的网站网站换主机换域名
  • 营销型网站有哪些建设流程怎样制作免费的网站
  • 天津建设工程计价网站手工加工网
  • 温州做美食网站网站建设的方案模板下载
  • 如何快速网站备案以用户为中心 建设学校网站
  • 宣传型网站有哪些宁波建设信息港网站
  • php网站开发是做什么的phpcms v9企业网站模板(简洁利于优化)
  • 什么是网站和网页wordpress启用插件出错
  • asp网站制作工具怎么样做国际网站生意
  • 签订网站建设合同山东建设工程招标网官方网站
  • 迅速建设企业网站外贸网站服务器选择
  • 建设网站详细流程wordpress建站数据库
  • 贵阳建立网站聊城网站建设设计
  • 网站怎么设置关键词百度网址大全首页设为首页
  • 中企动力网站怎么样怎么做公司内网网站
  • 求职网站网页模板一个网站可以做多少个小程序
  • 深圳市住房和建设局网站登录怎样在百度建网站