解决方案 »
- mysql修改/回收权限
- 有没有什么最简单方法 判断mysql某个数据库(如名称为aa)是否存在
- momery 表,重启MySQL时如何恢复里面的内容
- 求解:2003-can't connect MYSOL server on'local hodt'(10061)
- 请问这个PostgreSQL的SQL语句,怎么写?
- 如果用户忘记了口令,使用什么方法可以找回口令,请各位赐教!
- 简单的分页,为什么没有效果呢
- 关于mysql数据库
- 斑竹~~我想要你的qq~~~问一个mysql的解决并发的一个问题~~
- 关于Navicat Premium导入txt数据问题
- Mysql 的触发器问题
- 求大家帮忙解决一个问题......
user是用户
item是商品
数据库里面有三个表
user表
字段只有一个userid
item表
字段只有一个itemidrelation_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则没有处理
我可以肯定,我的问题一定是出在第二个游标的处理上,也就是红色的部分
可是我实在搞不清楚问题出在哪里,那位高人帮忙看一下
谢谢啦。
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;
curso_loop:loop
set done=0; 在第2个循环结束时,此值为1
在游标开始前赋值为0,FOR NOT FOUND SET done=1。分别判断各自的标志位。
我加了哪个set done=0;
但是还是老样子,没有任何变化
select count(*) from relation_used where itemid=myitemid into itemdegree;
update user set degreeself=degreeself+itemdegree where userid=myuserid;