牛骨文教育服务平台(让学习变的简单)
博文笔记

MySQL字符分割并存储到临时表中

创建时间:2016-12-01 投稿人: 浏览次数:158

创建存储过程

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测试查询结果




声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。