mysql存储过程,结果集遍历,if -else
来源:互联网 发布:深圳软件协会会长 编辑:程序博客网 时间:2024/06/06 07:26
BEGIN
#Routine body goes here...
#根据指定用户,统计用户关注,粉丝,黑名单个数
#用户关注或取消关注,拉黑或取消拉黑时调用该存储过程
DECLARE followCount BIGINT DEFAULT 0 ;
DECLARE fansCount BIGINT DEFAULT 0 ;
DECLARE blackCount BIGINT DEFAULT 0 ;
#创建接收游标数据的变量
declare c BIGINT;
declare n BIGINT(20);
#创建总数变量
declare total int default 0;
#创建结束标志变量
declare done int default false;
#创建游标
declare follow_cur cursor for select userid,count(*) count from attention GROUP BY userid;
declare fans_cur cursor for select attention_userid,count(*) count from attention GROUP BY attention_userid;
declare black_cur cursor for select userid,count(*) count from black GROUP BY userid;
#指定游标循环结束时的返回值
declare continue HANDLER for not found set done = true;
#传用户id时,统计该用户即可
IF uid != 0 THEN
SELECT COUNT(*) INTO followCount FROM attention where userid=uid ;
SELECT COUNT(*) INTO fansCount FROM attention where attention_userid=uid ;
SELECT COUNT(*) INTO blackCount FROM black where userid=uid ;
UPDATE u_user set attention=followCount,fans=fansCount,black_count=blackCount where id=uid;
ELSE
#不传用户id时,统计全部用户
#设置初始值
set total = 0;
#打开游标
open follow_cur;
#开始循环游标里的数据
read_loop:loop
#根据游标当前指向的一条数据
fetch follow_cur into c,n;
#判断游标的循环是否结束
if done then
leave read_loop; #跳出游标循环
end if;
#获取一条数据时,将count值进行累加操作,这里可以做任意你想做的操作,
update u_user set attention=n where id=c;
#输出结果
#结束游标循环
end loop;
#关闭游标
close follow_cur;
SET done = FALSE;-- 两个游标的情况下,注意在遍历第二个游标之前将done标志设为FALSE
open fans_cur;
#开始循环游标里的数据
read_loop:loop
#根据游标当前指向的一条数据
fetch fans_cur into c,n;
#判断游标的循环是否结束
if done then
leave read_loop; #跳出游标循环
end if;
#获取一条数据时,将count值进行累加操作,这里可以做任意你想做的操作,
update u_user set fans=n where id=c;
#输出结果
#结束游标循环
end loop;
#关闭游标
close fans_cur;
SET done = FALSE;-- 两个游标的情况下,注意在遍历第二个游标之前将done标志设为FALSE
open black_cur;
#开始循环游标里的数据
read_loop:loop
#根据游标当前指向的一条数据
fetch black_cur into c,n;
#判断游标的循环是否结束
if done then
leave read_loop; #跳出游标循环
end if;
#获取一条数据时,将count值进行累加操作,这里可以做任意你想做的操作,
update u_user set black_count=n where id=c;
#输出结果
#结束游标循环
end loop;
#关闭游标
close black_cur;
END IF;
END
#Routine body goes here...
#根据指定用户,统计用户关注,粉丝,黑名单个数
#用户关注或取消关注,拉黑或取消拉黑时调用该存储过程
DECLARE followCount BIGINT DEFAULT 0 ;
DECLARE fansCount BIGINT DEFAULT 0 ;
DECLARE blackCount BIGINT DEFAULT 0 ;
#创建接收游标数据的变量
declare c BIGINT;
declare n BIGINT(20);
#创建总数变量
declare total int default 0;
#创建结束标志变量
declare done int default false;
#创建游标
declare follow_cur cursor for select userid,count(*) count from attention GROUP BY userid;
declare fans_cur cursor for select attention_userid,count(*) count from attention GROUP BY attention_userid;
declare black_cur cursor for select userid,count(*) count from black GROUP BY userid;
#指定游标循环结束时的返回值
declare continue HANDLER for not found set done = true;
#传用户id时,统计该用户即可
IF uid != 0 THEN
SELECT COUNT(*) INTO followCount FROM attention where userid=uid ;
SELECT COUNT(*) INTO fansCount FROM attention where attention_userid=uid ;
SELECT COUNT(*) INTO blackCount FROM black where userid=uid ;
UPDATE u_user set attention=followCount,fans=fansCount,black_count=blackCount where id=uid;
ELSE
#不传用户id时,统计全部用户
#设置初始值
set total = 0;
#打开游标
open follow_cur;
#开始循环游标里的数据
read_loop:loop
#根据游标当前指向的一条数据
fetch follow_cur into c,n;
#判断游标的循环是否结束
if done then
leave read_loop; #跳出游标循环
end if;
#获取一条数据时,将count值进行累加操作,这里可以做任意你想做的操作,
update u_user set attention=n where id=c;
#输出结果
#结束游标循环
end loop;
#关闭游标
close follow_cur;
SET done = FALSE;-- 两个游标的情况下,注意在遍历第二个游标之前将done标志设为FALSE
open fans_cur;
#开始循环游标里的数据
read_loop:loop
#根据游标当前指向的一条数据
fetch fans_cur into c,n;
#判断游标的循环是否结束
if done then
leave read_loop; #跳出游标循环
end if;
#获取一条数据时,将count值进行累加操作,这里可以做任意你想做的操作,
update u_user set fans=n where id=c;
#输出结果
#结束游标循环
end loop;
#关闭游标
close fans_cur;
SET done = FALSE;-- 两个游标的情况下,注意在遍历第二个游标之前将done标志设为FALSE
open black_cur;
#开始循环游标里的数据
read_loop:loop
#根据游标当前指向的一条数据
fetch black_cur into c,n;
#判断游标的循环是否结束
if done then
leave read_loop; #跳出游标循环
end if;
#获取一条数据时,将count值进行累加操作,这里可以做任意你想做的操作,
update u_user set black_count=n where id=c;
#输出结果
#结束游标循环
end loop;
#关闭游标
close black_cur;
END IF;
END
阅读全文
0 0
- mysql存储过程,结果集遍历,if -else
- MySql 存储过程 IF ElSE 小实例
- MySql 存储过程 IF ElSE 小实例
- oracle存储过程if-else if-else
- mysql存储过程中if...else常见用法
- mysql进阶:存储过程中的IF语句(IF THEN ELSEIF THEN ELSE END IF)
- 存储过程中If Else的使用方法
- 存储过程中If Else的使用方法
- 存储过程中If Else的使用方法
- 存储过程调用 遍历返回结果集
- mysql中触发器和存储过程的if-else中的子句不能为空
- Mysql创建存储过程及遍历查询结果
- mysql 里面的 IF 判断 CASE 和存储过程 MYSQ IF ELSE 判断 时间添加 DATE_ADD 总结
- MYSQL存储过程遍历结果集游标 从一张表插入数据到另一张表
- Mat遍历,if and else if else
- MYSQL存储过程&&JAVA读取结果集
- Mysql利用存储过程获取结果集
- mysql 创建存储过程 返回结果集 调用存储过程
- 浅析可变参数
- 模型/视图实战之仿QQ好友列表面板
- java MessagePack的数据处理
- php接收post原始数据
- 计算a+b的和
- mysql存储过程,结果集遍历,if -else
- 利用同一个Activity界面实现二个不同的界面之间的跳转
- Android开发之高级渲染
- LevelDB:一个快速轻量级的key-value存储库(译)
- EventBus使用详解(一)——初步使用EventBus
- C# DataTable 用法
- 高性能队列Disruptor的使用
- 反汇编下的字符运算
- HDU4902 线段树