MySQL-procedure(cursor,loop)
现有一张表spam_keyword,共629条记录,每条记录的word字段的字符数量不等。
CREATE TABLE `spam_keyword` (
`kid` int(11) NOT NULL,
`word` varchar(255) DEFAULT NULL,
`styles` varchar(50) DEFAULT NULL,
`cids` varchar(100) DEFAULT NULL,
`is_active` smallint(6) DEFAULT NULL,
`tm_add` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,-- 不为空,创建新纪录时设为当前时间
PRIMARY KEY (`kid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
要写一个procedure的目的是:根据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;
完成后,call proc134f 就可以查看在t表中的结果
+-----+----+-----+
| id | t | cnt |
+-----+----+-----+
| 31 | 院 | 42 |
| 236 | 医 | 40 |
| 2 | 小 | 37 |
| 156 | 0 | 27 |
| 51 | 中 | 26 |
| 202 | 州 | 26 |
| 107 | 发 | 24 |
| 150 | 1 | 24 |
| 6 | 服 | 22 |
| 154 | 5 | 21 |
.
..
...省略1000行左右
| 1015 | 苑 | 1 |
| 1016 | 泽 | 1 |
| 1017 | 被 | 1 |
| 1018 | 驻 | 1 |
| 1019 | 鲁 | 1 |
| 1020 | 木 | 1 |
| 1021 | 齐 | 1 |
| 1022 | 雅 | 1 |
| 1023 | 友 | 1 |
+------+----+-----+
最终结果共1023行,即在spam_keyword表中共出现1023个不重复的字符,包含中文,英文,标点数字。出现次数最多的是“医“,”院”。
MySQL-procedure(cursor,loop)的更多相关文章
- MYSQL procedure
没怎么接触过mysql procedure,今天建个calendar表还磨磨唧唧的,记录一下: CREATE PROCEDURE `new_procedure` (start_date DATA,en ...
- MySQL Procedure(MySQL存储过程)[转]
------Creating Stored Procedures in MySQL------ --Make sure you have version 5 of MySQL: SELECT VE ...
- MySQL 中while loop repeat 的基本用法
-- MySQL中的三中循环 while . loop .repeat 求 1-n 的和 -- 第一种 while 循环 -- 求 1-n 的和 /* while循环语法: while 条件 DO 循 ...
- MySQL中 while loop repeat 的用法
-- MySQL中的三中循环 while . loop .repeat 求 1-n 的和 -- 第一种 while 循环 -- 求 1-n 的和 /* while循环语法: while 条件 DO 循 ...
- MySql存储过程 CURSOR循环
游标 游标(Cursor)是处理数据的一种方法,为了查看或者处理结果集中的数据,游标提供了在结果集中一次一行或者多行前进或向后浏览数据的能力. 使用步骤 声明一个游标: declare 游标名称 CU ...
- mysql 游标CURSOR
FETCH cursor_works INTO num,provinceIDs,cityIDs,SourceID; 定义的变量值必须与 游标中的字段不同,一一对应 DECLARE cursor_wor ...
- MySQL游标(cursor) 定义及使用
概念 游标(Cursor)它使用户可逐行访问由SQL Server返回的结果集. 使用游标(cursor)的一个主要的原因就是把集合操作转换成单个记录处理方式. 用SQL语言从数据库中检索数据后,结果 ...
- 初习mysql procedure
1.存储过程简介 我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的SQL语句集,经编译后存储在数据库中,用户 ...
- mysql游标cursor与for循环
delimiter // create procedure p2() begin declare row_id int DEFAULT 0; declare row_num int DEFAULT 0 ...
随机推荐
- 去除utf8文件的bom标记
http://stackoverflow.com/questions/1068650/using-awk-to-remove-the-byte-order-mark http://thegreyblo ...
- python问题记录
今天才python群里看到一个问题 python2.7: L = [x for x in 'hello'] print L print x python3.4: L = [ x for x in 'h ...
- Bzoj 2038---[2009国家集训队]小Z的袜子(hose) 莫队算法
题目链接 http://www.lydsy.com/JudgeOnline/problem.php?id=2038 Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色 ...
- 算法实质【Matrix67】
动态规划 :你追一个MM的时候,需要对该MM身边的各闺中密友都好,这样你追MM这个问题 就分解为对其MM朋友的问题,只有把这些问题都解决了,最终你才能追到MM. 因此,该问题适用于聪明的MM,懂得“看 ...
- [moka同学笔记]Yii2.0 modal的使用
第一次使用,时候不明白什么原理,大概用了几次后,才模模糊糊搞清楚原来是怎么一回事,现在就把写过的代码,贴在下边. 1.在视图文件中, 第一步首先在index.php文件中 做了一个a链接的按钮 调用了 ...
- Javascript 语言精粹 代码片段合集
Javascript 语言精粹 代码片段合集 标签:Douglas-Crockford Javascript 最佳实践 原文链接 更好的阅读体验 使用一个method 方法定义新方法 Function ...
- MasonJS – 创建完美的砌体结构网页布局
MasonJS 插件用来解决目前大多数的网格系统使用中的问题——间距.当使用 Masonry,Isotope 或任何其他网格插件时,布局中会出现空白或边缘参差不齐的情况.MasonJS 可以帮助你填补 ...
- .NET正则表达式匹配Silverlight
这是一个.NET正则表达式匹配工具的Silverlight 在页面中加入以下代码就可以了: <"> <param name="source" value ...
- Mac地址泛洪攻击的防御措施和具体配置
Mac地址泛洪攻击指的是:利用交换机的mac地址学习机制,攻击者不断地刷新mac地址,填满交换机的mac地址表,以致崩溃,使交换机不得不使用广播发包,从而获取其他人的报文信息. mac地址泛洪攻击的防 ...
- Wifite.py 修正版脚本代码
Kali2.0系统自带的WiFite脚本代码中有几行错误,以下是修正后的代码: #!/usr/bin/python # -*- coding: utf-8 -*- """ ...