基本表temp

包括 name, type, sendCity, getCity

分别对应物流送货司机名, 倒车的第几段, 发货城市, 收货城市

表结构

-- ----------------------------
-- Table structure for `temp`
-- ----------------------------
DROP TABLE IF EXISTS `temp`;
CREATE TABLE `temp` (
`id` int(16) NOT NULL AUTO_INCREMENT,
`name` varchar(32) DEFAULT NULL,
`type` varchar(32) DEFAULT NULL,
`sendCity` varchar(32) DEFAULT NULL,
`getCity` varchar(32) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of temp
-- ----------------------------
INSERT INTO `temp` VALUES ('', '张三', '', '北京', '沈阳');
INSERT INTO `temp` VALUES ('', '张三', '', '沈阳', '哈尔滨');
INSERT INTO `temp` VALUES ('', '李四', '', '保定', '天津');
INSERT INTO `temp` VALUES ('', '李四', '', '天津', '东莞');
INSERT INTO `temp` VALUES ('', '李四', '', '东莞', '广州');
INSERT INTO `temp` VALUES ('', '王五', '', '保定', '石家庄');
INSERT INTO `temp` VALUES ('', '王五', '', '石家庄', '宁晋');

这里是分段的,需要将姓名是“张三” 的第二,第三段合成一个 记录,也就是  “张三”   “北京” “哈尔滨”

物流司机可能是 1,2段,可能是1,2,3段,可能是2,3段所以在这里写一个存储过程,将合并后的结果放到另一个表中,

存储过程用到双游标嵌套,仅做记录

BEGIN

  DECLARE done INT DEFAULT 0;
DECLARE _name varchar(64);
declare _maxType varchar(32);
declare _minType varchar(32); declare _type varchar(64);
declare _sendCity varchar(64);
declare _getCity varchar(64); #declare _tempName varchar(64);
declare _tempSend varchar(64);
declare _tempGet varchar(64); DECLARE cursorOuter CURSOR for select name, max(type) maxType, min(type) minType from temp group by name;
DECLARE cursorInner CURSOR for select type, sendCity, getCity from temp where name=_name;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; #循环终止的标志,游标中如果没有数据就设置done为1 #select now();
OPEN cursorOuter;
FETCH cursorOuter INTO _name, _maxType, _minType;
WHILE (done=0) DO begin
open cursorInner;
fetch cursorInner into _type, _sendCity, _getCity; while (done=0) DO
if _type = _maxType then
set _tempGet = _getCity;
elseif _type = _minType then
set _tempSend = _sendCity;
end if;
FETCH cursorInner into _type, _sendCity, _getCity;
end while; close cursorInner;
end; INSERT into temp1(name, type, sendCity, getCity) values(_name, _type, _tempSend, _tempGet); SET done = 0;
## 赋值下一个游标
FETCH cursorOuter INTO _name, _maxType, _minType;
END WHILE; ## 关闭
CLOSE cursorOuter;
END

最后的 temp1 表结构和数据是这样的:

-- ----------------------------
-- Table structure for `temp1`
-- ----------------------------
DROP TABLE IF EXISTS `temp1`;
CREATE TABLE `temp1` (
`id` int(16) NOT NULL AUTO_INCREMENT,
`name` varchar(32) DEFAULT NULL,
`type` varchar(32) DEFAULT NULL,
`sendCity` varchar(32) DEFAULT NULL,
`getCity` varchar(32) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of temp1
-- ----------------------------
INSERT INTO `temp1` VALUES ('', '张三', '', '北京', '哈尔滨');
INSERT INTO `temp1` VALUES ('', '李四', '', '保定', '广州');
INSERT INTO `temp1` VALUES ('', '王五', '', '保定', '宁晋');

遇到的问题:

1、存储过成总是提示 nodata,查询后 添加

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; #循环终止的标志,游标中如果没有数据就设置done为1

SET done = 0;

while循环以WHILE (done=0) DO  作为判断。

2、游标嵌套后需要加

begin……end;

内层循环是包含在其中的;

mysql 存储过程 游标嵌套的更多相关文章

  1. MySQL存储过程 游标

    MySQL存储过程  游标 如何在存储过程中使用MySQL游标来遍历SELECT语句返回的结果集 MySQL游标简介 要处理存储过程中的结果集,请使用游标.游标允许您迭代查询返回的一组行,并相应地处理 ...

  2. mysql存储过程游标嵌套循环

    自己写的一个mysql存储过程如下: BEGIN DECLARE _did bigint(20);DECLARE _count int;DECLARE s1 int;DECLARE cur_1 CUR ...

  3. MySQL 存储过程/游标/事务

    将会用到的几个表 mysql> DESC products; +------------+--------------+------+-----+---------+-------------- ...

  4. php调用mysql存储过程游标

    <?php $dbtype = 'mysql'; $host = 'localhost'; $dbname = 'test'; $dsn = "$dbtype:host=$host;d ...

  5. mysql存储过程游标加计划任务事件调度器

    存储过程加事件调度器 -- 存储过程 (多个)游标的使用 临时表的使用(让执行时间从一个小时降低到5分钟)DELIMITER $$ DROP PROCEDURE IF EXISTS `eval_cal ...

  6. mysql 多重游标嵌套

    1.DECLARE CONTINUE HANDLER FOR NOT FOUND 在mysql的存储过程中经常会看到这句话:DECLARE CONTINUE HANDLER FOR NOT FOUND ...

  7. MySQL 存储过程游标

    一.创建游标 游标用declare语句创建.如下面的例子所示: create procedure test2() begin declare cursorTest cursor for select ...

  8. mysql存储过程 --游标的使用 取每行记录 (多字段)

    delimiter $ create PROCEDURE phoneDeal() BEGIN DECLARE id varchar(64); -- id DECLARE phone1 varchar( ...

  9. mysql 存储过程 -- 游标的使用(备忘)

    BEGIN ; DECLARE f_ratio FLOAT DEFAULT 0.8; ); ); DECLARE i_statDate DATE; DECLARE i_accumulateCount ...

随机推荐

  1. linux安装dpkg安装缺少依赖项的解决

    问题: dpkg: error processing package rxvt:i386 (--install): dependency problems - leaving unconfigured ...

  2. 2019.01.22 bzoj3333: 排队计划(逆序对+线段树)

    传送门 题意简述:给出一个序列,支持把ppp~nnn中所有小于等于apa_pap​的'扯出来排序之后再放回去,要求动态维护全局逆序对. 思路:我们令fif_ifi​表示第iii个位置之后比它大的数的个 ...

  3. 2019.01.08 bzoj3809: Gty的二逼妹子序列(莫队+权值分块)

    传送门 题意:多组询问,问区间[l,r]中权值在[a,b]间的数的种类数. 看了一眼大家应该都知道要莫队了吧. 然后很容易想到用树状数组优化修改和查询做到O(mnlogamax)O(m\sqrt nl ...

  4. hdu6444 2018中国大学生程序设计竞赛 - 网络选拔赛 1007 Neko's loop

    Neko's loop Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total S ...

  5. vue中的路由嵌套

    1定义组件 const Index = { template:` <div>首页</div> ` } const Order = { template:` <div> ...

  6. css 兼容ie8 rgba()用法

    今天遇到了一个问题,要在一个页面中设置一个半透明的白色div.这个貌似不是难题,只需要给这个div设置如下的属性即可: background: rgba(255,255,255,.1); 但是要兼容到 ...

  7. word粘贴图片+的editor

    公司做的项目需要用到文本上传功能. Chrome+IE默认支持粘贴剪切板中的图片,但是我要粘贴的文章存在word里面,图片多达数十张,我总不能一张一张复制吧? 我希望打开文档doc直接复制粘贴到富文本 ...

  8. Codeforces822 A I'm bored with life

    A. I'm bored with life time limit per test 1 second memory limit per test 256 megabytes input standa ...

  9. inline&friend&操作符重载

    (1).inline:是一种以空间换时间的做法省去调用函数的额外开销,提高程序的运行效率,它对于编译器而言只是一种建议 (2).友元函数:是可以直接访问类的private成员的非成员函数.它是定义在类 ...

  10. ORACLE EBS常用表

    http://www.cnblogs.com/quanweiru/archive/2012/09/26/2704628.html call fnd_global.APPS_INITIALIZE(131 ...