mysql存储过程中两个CURSOR嵌套使用遇到的有关问题
来源:互联网 发布:航盛导航端口 编辑:程序博客网 时间:2024/05/18 17:02
mysql存储过程中两个CURSOR嵌套使用遇到的问题
我的这个东西就是模拟用户购买商品的一个小东西
user是用户
item是商品
数据库里面有三个表
user表
字段只有一个userid
item表
字段只有一个itemid
relation_used,记录user和item关系
我现在需要做的是这个事情
1.根据一个userid,查询这个user够买过哪些item
2.针对上一步查询出来的每个item,再查询这些item分别被那些user买过,然后对每个uer进行进一步处理
代码如下
CREATE DEFINER=`root`@`localhost` PROCEDURE `second1`(
IN id INTEGER(11)
)
BEGIN
/*保存itemid*/
declare myitemid int;
/*保存userid*/
declare myuserid int;
/*保存度*/
declare itemdegree float default 0;
/*当游标走到最后,设置done为1*/
declare done int default 0;
/*查询一个user所购买的所有商品的id*/
DECLARE rs_cursor CURSOR FOR select itemid from relation_used where userid=4;
/*查询购买过myitemid,这个myitemid是第一个游标查询的结果。*/
DECLARE mycursor CURSOR FOR select userid from relation_used where itemid=myitemid;[/color]
/*当游标走到最后,设置done为1*/
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
/*打开第一个游标开始循环,查询userid=4这个用户都买过那些item*/
open rs_cursor;
curso_loop:loop
/*获得一个itemid*/
FETCH rs_cursor into myitemid;
if done=1 then
leave curso_loop;
end if;
/*记录这个itemid的度*/
select count(*) from relation_used where itemid=myitemid into itemdegree;
/*打开第2个游标开始循环,对于每个item,在查询那些user购买过这个item*/
open mycursor;
mycursor_loop:loop
/*获得一个userid*/
FETCH mycursor into myuserid;
if done=1 then
leave mycursor_loop;
end if;
update user set degreeself=degreeself+itemdegree where userid=myuserid;
END LOOP;
close mycursor;
end loop curso_loop;
close rs_cursor;
END;
这里,第一个游标查询uerid=4分别买过那些商品,这个一步没有问题,我验证过了。这里假设查询结果是itemid分别为1,2,3
但是对于第二个游标就出现问题了,按说它应该对第一个游标查询出来的结果集合itemid(1,2,3)分别进行处理,但是实际上,它只处理了itemid=1这一种情况,对于第一步查询出来的2和3则没有处理
我可以肯定,我的问题一定是出在第二个游标的处理上,也就是红色的部分
可是我实在搞不清楚问题出在哪里,那位高人帮忙看一下
谢谢啦。
------解决方案--------------------
CREATE DEFINER=`root`@`localhost` PROCEDURE `second1`(
IN id INTEGER(11)
)
BEGIN
declare myitemid int;
declare myuserid int;
declare itemdegree float default 0;
declare done int default 0;
DECLARE rs_cursor CURSOR FOR select itemid from relation_used where userid=4;
DECLARE mycursor CURSOR FOR select userid from relation_used where itemid=myitemid;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
open rs_cursor;
curso_loop:loop
set done=0; -- 加上这句。
FETCH rs_cursor into myitemid;
if done=1 then
leave curso_loop;
end if;
select count(*) from relation_used where itemid=myitemid into itemdegree;
open mycursor;
mycursor_loop:loop
FETCH mycursor into myuserid;
if done=1 then
leave mycursor_loop;
end if;
update user set degreeself=degreeself+itemdegree where userid=myuserid;
END LOOP;
close mycursor;
end loop curso_loop;
close rs_cursor;
END;
------解决方案--------------------
可以,不过 要注意DONE的值
curso_loop:loop
set done=0; 在第2个循环结束时,此值为1
------解决方案--------------------
你可以给每个游标都设置它们单独的循环结束标志位。如DONE1,DONE2,DONE3等。
在游标开始前赋值为0,FOR NOT FOUND SET done=1。分别判断各自的标志位。
------解决方案--------------------
检查是否有满足条件的记录
select count(*) from relation_used where itemid=myitemid into itemdegree;
update user set degreeself=degreeself+itemdegree where userid=myuserid;
我的这个东西就是模拟用户购买商品的一个小东西
user是用户
item是商品
数据库里面有三个表
user表
字段只有一个userid
item表
字段只有一个itemid
relation_used,记录user和item关系
我现在需要做的是这个事情
1.根据一个userid,查询这个user够买过哪些item
2.针对上一步查询出来的每个item,再查询这些item分别被那些user买过,然后对每个uer进行进一步处理
代码如下
CREATE DEFINER=`root`@`localhost` PROCEDURE `second1`(
IN id INTEGER(11)
)
BEGIN
/*保存itemid*/
declare myitemid int;
/*保存userid*/
declare myuserid int;
/*保存度*/
declare itemdegree float default 0;
/*当游标走到最后,设置done为1*/
declare done int default 0;
/*查询一个user所购买的所有商品的id*/
DECLARE rs_cursor CURSOR FOR select itemid from relation_used where userid=4;
/*查询购买过myitemid,这个myitemid是第一个游标查询的结果。*/
DECLARE mycursor CURSOR FOR select userid from relation_used where itemid=myitemid;[/color]
/*当游标走到最后,设置done为1*/
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
/*打开第一个游标开始循环,查询userid=4这个用户都买过那些item*/
open rs_cursor;
curso_loop:loop
/*获得一个itemid*/
FETCH rs_cursor into myitemid;
if done=1 then
leave curso_loop;
end if;
/*记录这个itemid的度*/
select count(*) from relation_used where itemid=myitemid into itemdegree;
/*打开第2个游标开始循环,对于每个item,在查询那些user购买过这个item*/
open mycursor;
mycursor_loop:loop
/*获得一个userid*/
FETCH mycursor into myuserid;
if done=1 then
leave mycursor_loop;
end if;
update user set degreeself=degreeself+itemdegree where userid=myuserid;
END LOOP;
close mycursor;
end loop curso_loop;
close rs_cursor;
END;
这里,第一个游标查询uerid=4分别买过那些商品,这个一步没有问题,我验证过了。这里假设查询结果是itemid分别为1,2,3
但是对于第二个游标就出现问题了,按说它应该对第一个游标查询出来的结果集合itemid(1,2,3)分别进行处理,但是实际上,它只处理了itemid=1这一种情况,对于第一步查询出来的2和3则没有处理
我可以肯定,我的问题一定是出在第二个游标的处理上,也就是红色的部分
可是我实在搞不清楚问题出在哪里,那位高人帮忙看一下
谢谢啦。
------解决方案--------------------
CREATE DEFINER=`root`@`localhost` PROCEDURE `second1`(
IN id INTEGER(11)
)
BEGIN
declare myitemid int;
declare myuserid int;
declare itemdegree float default 0;
declare done int default 0;
DECLARE rs_cursor CURSOR FOR select itemid from relation_used where userid=4;
DECLARE mycursor CURSOR FOR select userid from relation_used where itemid=myitemid;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
open rs_cursor;
curso_loop:loop
set done=0; -- 加上这句。
FETCH rs_cursor into myitemid;
if done=1 then
leave curso_loop;
end if;
select count(*) from relation_used where itemid=myitemid into itemdegree;
open mycursor;
mycursor_loop:loop
FETCH mycursor into myuserid;
if done=1 then
leave mycursor_loop;
end if;
update user set degreeself=degreeself+itemdegree where userid=myuserid;
END LOOP;
close mycursor;
end loop curso_loop;
close rs_cursor;
END;
------解决方案--------------------
可以,不过 要注意DONE的值
curso_loop:loop
set done=0; 在第2个循环结束时,此值为1
------解决方案--------------------
你可以给每个游标都设置它们单独的循环结束标志位。如DONE1,DONE2,DONE3等。
在游标开始前赋值为0,FOR NOT FOUND SET done=1。分别判断各自的标志位。
------解决方案--------------------
检查是否有满足条件的记录
select count(*) from relation_used where itemid=myitemid into itemdegree;
update user set degreeself=degreeself+itemdegree where userid=myuserid;
0 0
- mysql存储过程中两个CURSOR嵌套使用遇到的有关问题
- mysql存储过程中两个CURSOR嵌套使用
- Cursor使用过程中遇到的一个问题
- Mysql存储过程中使用cursor
- mysql存储过程中遇到的问题
- MYSQL使用过程中遇到的问题
- MySQL 的存储过程写法和Cursor的使用
- MySQL 的存储过程写法和Cursor的使用
- 用mysql写存储过程中遇到的问题
- mysql存储过程 例子 游标cursor 循环嵌套 事物
- mysql存储过程 例子 游标cursor 循环嵌套 事物
- mysql存储过程 例子 游标cursor 循环嵌套 事物
- MYSQL存储过程循环CURSOR(游标)使用
- 存储过程中 游标(CURSOR)的简单使用及其他
- 存储过程中 游标(CURSOR)的简单使用及其他
- SQL Server学习:存储过程中Cursor(游标)的使用
- MySql学习存储过程遇到的问题
- MySql存储过程遇到的问题
- svn: 方法 OPTIONS 失败于 “https://192.168.1.110/svn/amlogic_mx/software/turnkey_src”: SSL handshake fail
- MyBatis_与spring的整合之添加事务
- struts2 拦截器 登录权限验证
- 03 Linux中C语言头文件位置
- 吞吐量(Throughput)、QPS、并发数、响应时间(RT)对系统性能的影响
- mysql存储过程中两个CURSOR嵌套使用遇到的有关问题
- Jasper Report v1.0用户手册中文版
- 基于模型的测试框架的想法 (二)
- MTK平台camera AF马达驱动调试流程分析
- PHP post 接口上传图片 [接收ios ||androip 两种接收图片数据流保存图片方式]
- ZOJ2925 DomiNo Grid(广搜)
- dedecms 调试
- Android的图片压缩类ThumbnailUtils
- 分享六个基于Bootstrap的实用开发教程和模板演示