今天修改了所有产品的图片信息,用到了存储过程。在参考下面存储过程以后,终于搞定了。
1 BEGIN 2 DECLARE Done INT DEFAULT 0; 3 4 DECLARE CurrentLingQi INT; 5 6 DECLARE ShizuName VARCHAR(30); 7 /* 声明游标 */ 8 DECLARE rs CURSOR FOR SELECT NodeName, LingQi FROM socialrelation; 9 /* 异常处理 */ 10 DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET Done = 1; 11 12 /* 打开游标 */ 13 OPEN rs; 14 15 /* 逐个取出当前记录LingQi字段的值,需要进行最大值的判断 */ 16 FETCH NEXT FROM rs INTO ShizuName, CurrentLingQi; 17 /* 遍历数据表 */ 18 REPEAT 19 IF NOT Done THEN 20 SET CurrentLingQi = CurrentLingQi + 60; 21 /* 如果更新后灵气值大于允许的最大值,则就设置为最大值 */ 22 IF CurrentLingQi >= 1800 THEN 23 UPDATE socialrelation SET LingQi = 1800 WHERE NodeName = ShizuName; 24 ELSE 25 /* 否则,正常更新 */ 26 UPDATE socialrelation SET LingQi = LingQi + 60 WHERE NodeName = ShizuName; 27 END IF; 28 END IF; 29 30 FETCH NEXT FROM rs INTO ShizuName, CurrentLingQi; 31 32 UNTIL Done END REPEAT; 33 34 /* 关闭游标 */ 35 CLOSE rs; 36 END
把写的存储过程也记录下来,作为以后参考。在感谢罗浮宫的童鞋们。感谢伊罗生。
begin DECLARE Done INT DEFAULT 0; declare pid int(11); declare miaoshu text; declare topnamePosition int(11); declare startTopname int(11); declare revPos int(11); declare miaoshuLength int(11); declare endTopname int(11); declare pname VARCHAR(255); declare topnameLength text; declare topnameTag text; declare reverseMiaoshu text; declare rs cursor for select productid,productname,chanpinmiaoshu from product; declare CONTINUE HANDLER FOR SQLSTATE '02000' SET Done = 1; open rs; fetch next from rs into pid,pname,miaoshu; repeat if not done then set miaoshuLength=CHARACTER_LENGTH(miaoshu); set topnamePosition=position("13733681492079.jpg" in miaoshu); set endTopname=locate(">",miaoshu,topnamePosition); set reverseMiaoshu=reverse(miaoshu); set revPos=miaoshuLength-topnamePosition; set startTopname=locate("gmi<",reverseMiaoshu,revPos); set startTopname=miaoshuLength-startTopname; set topnameLength=endTopname-startTopname; set topnameTag=substring(miaoshu,startTopname-2,topnameLength+3); select pid,topnameTag; update product set `chanpinmiaoshu`=replace(`chanpinmiaoshu`,trim(topnameTag),'<img src="/ueditor/php/upload/20130709/13733681492079.jpg" width="740" height="651" border="0" hspace="0" vspace="0" style="740px;height:651px;" alt="熊猫银币投资">') where productid=pid; end if; fetch next from rs into pid,pname,miaoshu; until done end repeat; close rs; end