mysql 拆分A表字段存到临时表
问题描述:
mysql 拆分A表字段存到临时表
现有表 tba
求拆分成tbb
答
SELECT t.id as id, substring_index(substring_index(t.str,',', b.help_topic_id + 1), ',', -1) as str
FROM tba t join mysql.help_topic b ON b.help_topic_id < (LENGTH(t.str) - LENGTH(REPLACE(t.str, ',', '')) + 1);
答
跪求大神帮忙。急急急急。
答
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