現有一張表spam_keyword,共629條記錄,每條記錄的word字段的字符數量不等。oop
1 CREATE TABLE `spam_keyword` ( 2 `kid` int(11) NOT NULL, 3 `word` varchar(255) DEFAULT NULL, 4 `styles` varchar(50) DEFAULT NULL, 5 `cids` varchar(100) DEFAULT NULL, 6 `is_active` smallint(6) DEFAULT NULL, 7 `tm_add` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,-- 不爲空,建立新紀錄時設爲當前時間 8 PRIMARY KEY (`kid`) 9 ) ENGINE=MyISAM DEFAULT CHARSET=utf8
要寫一個procedure的目的是:根據spam_keyword表中的kid對word列逐行逐字分割到t表中,而且統計每一個字的數量。fetch
過程當中用了兩層循環,外循環遊標讀行,內循環stroo++讀字。spa
1 drop procedure if exists proc134f; 2 create procedure proc134f() 3 begin 4 declare kidoo int; 5 declare tid int; 6 declare stroo int; 7 declare str varchar(255); 8 declare strlength int; 9 declare istr varchar(3); 10 declare istr_cnt int; 11 declare done int; -- 遊標用 12 declare cur_kid cursor for select kid from spam_keyword; -- 遊標用 13 declare continue handler for not found set done=1; -- 遊標用,聽說是直達引擎的通道 14 set tid=0; 15 delete from t; 16 open cur_kid; -- 開啓遊標 17 loop1:loop -- 開啓1層外循環 18 fetch cur_kid into kidoo; -- 從定義的範圍中讀取下一行並賦予 19 if done=1 then leave loop1;end if; -- 判斷是否 found 下一行,不然跳出 20 select word into str from spam_keyword where kid=kidoo; 21 set str=replace(str,' ',''); -- 去掉空格 22 set strlength=char_length(str); 23 set stroo=0; 24 loop2:loop -- 2層內循環 25 set stroo=stroo+1; 26 set istr=substr(str,stroo,1); 27 select count(*) into istr_cnt from t where t=istr; -- 計數 28 if istr_cnt<>0 then update t set cnt=cnt+1 where t=istr;else 29 set tid=tid+1; 30 insert into t set id=tid,t=istr,cnt=1;end if; 31 if stroo>=strlength then leave loop2;end if;end loop loop2; 32 33 set done=0; 34 end loop loop1; 35 close cur_kid; -- 關閉遊標 36 select * from t order by cnt desc; 37 END;
完成後,call proc134f 就能夠查看在t表中的結果code
1 +-----+----+-----+ 2 | id | t | cnt | 3 +-----+----+-----+ 4 | 31 | 院 | 42 | 5 | 236 | 醫 | 40 | 6 | 2 | 小 | 37 | 7 | 156 | 0 | 27 | 8 | 51 | 中 | 26 | 9 | 202 | 州 | 26 | 10 | 107 | 發 | 24 | 11 | 150 | 1 | 24 | 12 | 6 | 服 | 22 | 13 | 154 | 5 | 21 | 14 . 15 .. 16 ...省略1000行左右 17 | 1015 | 苑 | 1 | 18 | 1016 | 澤 | 1 | 19 | 1017 | 被 | 1 | 20 | 1018 | 駐 | 1 | 21 | 1019 | 魯 | 1 | 22 | 1020 | 木 | 1 | 23 | 1021 | 齊 | 1 | 24 | 1022 | 雅 | 1 | 25 | 1023 | 友 | 1 | 26 +------+----+-----+
最終結果共1023行,即在spam_keyword表中共出現1023個不重複的字符,包含中文,英文,標點數字。出現次數最多的是「醫「,」院」。blog