mysql中輪回截取用戶信息並拔出到目的表對應的字段中。本站提示廣大學習愛好者:(mysql中輪回截取用戶信息並拔出到目的表對應的字段中)文章只能為提供參考,不一定能成為您想要的結果。以下是mysql中輪回截取用戶信息並拔出到目的表對應的字段中正文
操作情況:有表game_list,字段:uid,score1,score2,seat_id,last_update;
傳入參數為i_player_detail ,傳入的值為多個用戶的id、之前分數、以後分數、坐位號,每一個用戶的數據用分號(;)離隔;
操作目標:將各個用戶對應的屬性拔出到目的表對應的字段中,last_update為數據更新日期;
傳入參數i_player_detail ,外面寄存多個用戶的信息,每一個用戶的一組數據用分號離隔,每一個用戶的信息多個,好比
“用戶id,score,desk,seat;
用戶id,score,desk,seat;……”
-- 應用存儲進程
delimiter $$
use `log_pdk`$$
drop procedure if exists `game_c`$$
create procedure `game_c` (in i_player_detail varchar(500))
SQL SECURITY INVOKER
BEGIN
DROP TABLE IF EXISTS `temp_list`;
--創立暫時表,將截取的數據先拔出莅臨時表
CREATE TEMPORARY TABLE `temp_list`(
`uid` INT(10) UNSIGNED NOT NULL,
`score1` INT(10) UNSIGNED NOT NULL,
`score2` INT(10) UNSIGNED NOT NULL,
`seat_id` TINYINT(3) UNSIGNED NOT NULL
);
-- declare str varchar(500);-- 用來拼接sql靜態語句
declare m_detail varchar(500);
declare m_num tinyint;
-- 當傳入的用戶信息字符串中含有分號';',停止截取
set m_num = position(';' in str) -- 不存在分號的時刻,前往0
while m_num >= 1 do
begin
set @str = 'insert into temp_list values (' + substring(m_detail,1,m_num-1)+')' -- 截取第一個用戶的信息(第一個分號後面的字符),拔出莅臨時表
prepare statement1 from @str;
execute statement1;
deallocate prepare statement1;
set m_detail = substring(m_detail,m_num+1); -- 界說除去第一個用戶和分號那部門的字符串
set set m_num = position(';' in str);
end while;
-- 從暫時表抽出一切字段,添加時光字段,拔出到表game_list
INSERT INTO `game_list`(`uid`,`score1`,`score2`,`seat_id`, `last_update`)
SELECT `uid`, `score1`, `score2`, `seat_id`, current_date()
FROM `temp_list`;
end$$
delimiter ;