博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
存储过程 利用游标 解决复制业务
阅读量:4687 次
发布时间:2019-06-09

本文共 954 字,大约阅读时间需要 3 分钟。

需求:

根据同一表的某条件更新字段

情况一: 以group by 作为条件的

select user a, (select count(*) as tj_num, s_id from user group by s_id) b set a.tuijian_num=b.tj_num where a.id=b.s_id;

情况二: 不能以group by 作为条件的

#调用方法:call proc_update_team_info();DELIMITER ;;CREATE  PROCEDURE `proc_update_team_info`()BEGIN       DECLARE done tinyint default 0;    DECLARE uid int(11);    DECLARE team_num int DEFAULT 0;    DECLARE user_tree text DEFAULT '';    DECLARE cs CURSOR FOR SELECT id FROM `user` order by id asc;    DECLARE continue handler for sqlstate '02000' set done=1;    open cs;    while done<>1 do    fetch cs into uid;        select CONCAT(s_tree,id,',%') INTO user_tree  from `user` where id=uid;        select user_tree;        select if(count(*) is null,0,count(*)) into team_num  from `user` where s_tree like(user_tree);        update `user` set team_num=team_num where id=uid;    end while;    close cs;   END;;DELIMITER ;

转载于:https://www.cnblogs.com/xielisen/p/9987597.html

你可能感兴趣的文章
Windows下PyMC安装
查看>>
解决Ubantu系统下sublime无法输入中文的问题
查看>>
自动化测试的点点滴滴经验积累
查看>>
窗体布局
查看>>
第一章 Java代码执行流程
查看>>
自学JAVA-11:IO流
查看>>
Python实现装饰模式的一段代码
查看>>
漫画:女生/男生告白攻略
查看>>
MyBatis学习笔记
查看>>
表格 滚动条 (tbody部分滚动)
查看>>
SAStruts中文文档
查看>>
平时二十五测
查看>>
history。go(-1)
查看>>
TTThumbsViewController 由 4张 改为 3张的方式 增补
查看>>
flex invalidation 机制
查看>>
HTML5本地存储——IndexedDB二:索引
查看>>
C# 异步查询数据库(第二版)
查看>>
MYSQL问题解决方案:Access denied for user 'root'@'localhost' (using password:YES)
查看>>
webpack 的使用教程
查看>>
用代码排出自己的名字
查看>>