Home  >  Article  >  Database  >  统计分析-mysql游标_MySQL

统计分析-mysql游标_MySQL

WBOY
WBOYOriginal
2016-06-01 13:32:33816browse

bitsCN.com

统计分析-mysql游标

 

项目中的统计分析模块,使用存储过程对数据进行统计,满足用户查看报表数据的需求。之前对于mysql的游标使用较少,写个小例子,熟悉下游标以备忘。  

 

实现功能: 将shop_boss表中部分字段的值插入到test表中。

 

Sql代码  

CREATE DEFINER=`admin`@`%` PROCEDURE `test_cursor1`()  

BEGIN  

    #Routine body goes here...  

    DECLARE boss_count INT DEFAULT 0;#循环变量  

    DECLARE _boss_id INT(11);  

    DECLARE _user_id VARCHAR(10);  

    DECLARE _shop_id INT(11);  

    DECLARE _count INT DEFAULT 0;  

  

    DECLARE cur1 CURSOR FOR select boss_id,user_id,shop_id from shop_boss;#声明游标  

    OPEN cur1;#打开游标  

  

    select count(*) into _count from shop_boss;  

      

    WHILE boss_count

        FETCH cur1 INTO _boss_id, _user_id, _shop_id;  

        #select _boss_id;  

          

        insert into test values(_boss_id, _user_id, _shop_id);#插入数据  

        set boss_count = boss_count + 1;  

    END WHILE;  

  

    CLOSE cur1;#关闭游标  

  

END  

 

bitsCN.com
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn