統計分析-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 < _count DO#循環條件 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