MySQL字符分割並存儲到臨時表中

創建存儲過程

CREATE DEFINER=`root`@`localhost` PROCEDURE `split`(in _string varchar(300))
BEGIN
# 求分割符號','的位置
declare _index int;

#使用臨時表存儲分割後的結果
drop temporary table if exists tmp_strs;
create temporary table tmp_strs(
str int(10) unsigned
);

set _index = locate(',',_string);
while _index > 0
do
insert into tmp_strs values(left(_string,_index-1));#將子字符串存入臨時表
set _string =substr(_string from _index+1);
set _index = locate(',',_string);
end while;

if length(_string) >= 0 then
insert into tmp_strs values(_string);
end if;

END

在workbench測試查詢結果




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章