欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MySQL雙層游標(biāo)嵌套循環(huán)實(shí)現(xiàn)方法

 更新時間:2024年05月05日 11:06:50   作者:bacawa  
要實(shí)現(xiàn)逐行獲取數(shù)據(jù),需要用到MySQL中的游標(biāo),一個游標(biāo)相當(dāng)于一個for循環(huán),這里需要用到2個游標(biāo),如何在MySQL中實(shí)現(xiàn)游標(biāo)雙層循環(huán)呢,下面小編給大家分享MySQL雙層游標(biāo)嵌套循環(huán)方法,感興趣的朋友跟隨小編一起看看吧

1、需求描述

1、在項(xiàng)目中,需要將A表中主鍵id,逐個取出,作為條件,在B表中去逐一查詢,將B表查詢到的結(jié)果集(A表B表關(guān)系:一對多),逐一遍歷,連同A表的id拼接運(yùn)算,逐個插入到C表中。

2、 在Java中很容易實(shí)現(xiàn),A表獲取到的結(jié)果集,循環(huán)遍歷取出id,去B表查詢;遍歷B表結(jié)果集,插入到C表中。 相當(dāng)于2個循環(huán),即可實(shí)現(xiàn)需求。 這樣會有一個問題,頻繁連接數(shù)據(jù)庫,造成大量資源開銷。 那么在存儲過程中,該怎么實(shí)現(xiàn)呢?

2、思路

  要實(shí)現(xiàn)逐行獲取數(shù)據(jù),需要用到MySQL中的游標(biāo),一個游標(biāo)相當(dāng)于一個for循環(huán),這里需要用到2個游標(biāo)。如何在MySQL中實(shí)現(xiàn)游標(biāo)雙層循環(huán)呢?

3、創(chuàng)建存儲過程

CREATE DEFINER=`root`@`%` PROCEDURE `student`()
BEGIN
	-- 定義變量
		-- 假設(shè)有一張學(xué)生表,有id,student_name字段
		DECLARE outer_done INT DEFAULT FALSE; -- 外層游標(biāo)控制變量
		DECLARE studentTableId int;    -- 學(xué)生表ID
		declare studentTableName VARCHAR(100);   -- 學(xué)生姓名
		declare outer_cursor cursor for select id,student_name from student_table  where `disable` = '0'; 
		DECLARE CONTINUE HANDLER FOR NOT FOUND SET outer_done = TRUE;
		open outer_cursor;     
			while not outer_done do
					fetch outer_cursor into studentTableId,studentTableName;  -- 這里循環(huán)取值,賦值到上面定義的變量中
						-- 開始定義內(nèi)層游標(biāo)
						BEGIN -- inner BEGIN
								-- 假設(shè)有一張成績表,包含字段id,student_name,score字段
								DECLARE scoreTableId int;    -- 成績Id
								declare scoreTableName VARCHAR(100);    -- 學(xué)生名字
								declare scoreTableScore float;   -- 學(xué)生分?jǐn)?shù)
								DECLARE inner_done int DEFAULT FALSE ;
								DECLARE my_value VARCHAR(255);
								declare inner_cursor cursor for select id,student_name,score from score_table where `disable` = '0'; 
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET inner_done  = TRUE ;
								OPEN inner_cursor; -- 打開內(nèi)層游標(biāo)
								WHILE not inner_done DO -- inner WHILE
									FETCH inner_cursor INTO scoreTableId,scoreTableName,scoreTableScore ; -- 從【內(nèi)層游標(biāo)】中獲取數(shù)據(jù),賦值到定義變量中
									 IF studentTableName = scoreTableName THEN    -- 判斷名字一樣(測試,生產(chǎn)不要用名稱進(jìn)行判斷)
										set my_value = CONCAT_WS('-',studentTableName,scoreTableScore);    -- 給變量賦值 CONCAT_WS函數(shù)可以按照固定的連接符,將數(shù)據(jù)進(jìn)行連接,例如 張三-95
										select my_value;     -- 打印變量值
									 END IF;
									 -- 假設(shè)有一張匯總表(summary_table),將處理的數(shù)據(jù)進(jìn)行更新
									 update summary_table set summary_column=my_value where summary_table_student_id=studentTableId;
								END WHILE ; -- END inner WHILE
								CLOSE inner_cursor; -- 循環(huán)結(jié)束后,關(guān)閉內(nèi)層游標(biāo)
						END; -- END inner BEGIN
			end while;        
		close outer_cursor;  
END

看圖清晰一點(diǎn)。

到這里就完成了,存儲過程里面的注釋很詳細(xì),就不多贅述了,我在寫存儲過程中也是踩了不少坑,記錄下來,希望能幫到各位coder。

到此這篇關(guān)于MySQL雙層游標(biāo)嵌套循環(huán)方法的文章就介紹到這了,更多相關(guān)mysql游標(biāo)嵌套循環(huán)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論