当前位置:Gxlcms > mysql > MySQL-procedure小试牛刀_MySQL

MySQL-procedure小试牛刀_MySQL

时间:2021-07-01 10:21:17 帮助过:22人阅读

根据spam_keyword表中的kid对word列逐行逐字分割到t表中,并且统计每个字的数量。

用了两层循环,外循环游标读行,内循环stroo++读字。

drop procedure if exists proc134f;
CREATE PROCEDURE proc134f()
BEGIN
declare kidoo int;
declare tid int;
declare stroo int;
declare str varchar(255);
declare strlength int;
declare istr varchar(3);
declare istr_cnt int;
declare done int;-- 游标用
declare cur_kid CURSOR for select kid from spam_keyword;-- 游标用
declare continue handler for not found set done=1;-- 游标用,据说是直达引擎的通道
set tid=0;
delete from t;
open cur_kid;-- 开启游标
loop1:loop-- 开启1层外循环
fetch cur_kid into kidoo;    -- 从定义的范围中读取下一行并赋予
if done=1 then leave loop1;end if;-- 判断是否 found 下一行,否则跳出
select word into str from spam_keyword where kid=kidoo;
set str=replace(str,' ','');
set strlength=char_length(str);
set stroo=0;
loop2:loop-- 2层内循环
set stroo=stroo+1;
set istr=substr(str,stroo,1);
select count(*) into istr_cnt from t where t=istr;-- 计数
if istr_cnt<>0 then update t set cnt=cnt+1 where t=istr;else
set tid=tid+1;
insert into t set id=tid,t=istr,cnt=1;end if;
if stroo>=strlength then leave loop2;end if;end loop loop2;

set done=0;
end loop loop1;
close cur_kid;-- 关闭游标
select * from t order by cnt desc;
END;

人气教程排行