MySql字符串拆分实现split功能(字段分割转列)

需求描述

数据库中 num字段值为:

实现的效果:需要将一行数据变成多行

实现的sql

select substring_index(substring_index('7654,7698,7782,7788',',',help_topic_id+1),',',-1) as num 
from mysql.help_topic 
where help_topic_id < length('7654,7698,7782,7788')-length(replace('7654,7698,7782,7788',',',''))+1

案例演示

create table `company` (
`id` int(20) default null,
`name` varchar(100) default null,
`shareholder` varchar(100) default null
) engine=innodb default charset=utf8;
insert into `company` values ('1', '阿里巴巴', '马云');
insert into `company` values ('2', '淘宝', '马云,孙正义');

1、原始数据演示

2、处理结果演示

3、sql语句

select a.id
    , a.name
    , substring_index(substring_index(a.shareholder, ',', b.help_topic_id + 1), ',', - 1) as shareholder
from company a
inner join mysql.help_topic b
    on b.help_topic_id < (length(a.shareholder) - length(replace(a.shareholder, ',', '')) + 1)

涉及的知识点如下:

字符串拆分: substring_index(str, delim, count)

参数解说       解释
str         需要拆分的字符串
delim         分隔符,通过某字符进行拆分
count          当 count 为正数,取第 n 个分隔符之前的所有字符; 当 count 为负数,取倒数第 n 个分隔符之后的所有字符。

举例

(1)获取第2个以逗号为分隔符之前的所有字符。

select substring_index('7654,7698,7782,7788',',',2);

(2)获取最后一个到倒数第2个以逗号分隔符之后的所有字符

select substring_index('7654,7698,7782,7788',',',-2);

所以,我们的核心代码中的 -1 ,就是获取以逗号为分隔符的最后一个值;也就是7788

替换函数:replace( str, from_str, to_str)

参数名       解释
str        需要进行替换的字符串
from_str     需要被替换的字符串
to_str       需要替换的字符串

举例

将分隔符逗号替换为空。

select replace('7654,7698,7782,7788',',','');

获取字符串长度:length( str )

参数名   解释
str     需要计算长度的字符串

获取 ‘7654,7698,7782,7788’ 字符串的长度

select length('7654,7698,7782,7788')

实现的原理解析

【4.1】 实现sql

select substring_index(substring_index('7654,7698,7782,7788',',',help_topic_id+1),',',-1) as num 
from mysql.help_topic 
where help_topic_id < length('7654,7698,7782,7788')-length(replace('7654,7698,7782,7788',',',''))+1

此处利用 mysql 库的 help_topic 表的 help_topic_id 来作为变量,因为 help_topic_id 是自增的,当然也可以用其他表的自增字段辅助。

help_topic 表:

注意,这个辅助表的id最大长度只有658;如果过长的字符串,可能需要借助其他自增的辅助表(可以是现有表,也可以自己造一个 1,2,3,4 递增的行即可)

【4.2】正式的原理解析

原sql

select substring_index(substring_index('7654,7698,7782,7788',',',help_topic_id+1),',',-1) as num 
from   mysql.help_topic 
where  help_topic_id < length('7654,7698,7782,7788')-length(replace('7654,7698,7782,7788',',',''))+1

step1:首先获取最后需被拆分成多少个字符串,利用 help_topic_id 来模拟遍历 第n个字符串。

这一步核心就是获取,有多少个分隔符,比如本文的案例,就是知道有多少个逗号

涉及的代码片段:

help_topic_id < length('7654,7698,7782,7788')-length(replace('7654,7698,7782,7788',',',''))+1

因为 help_topic_id是从0开始的,所以会得出 help_topic_id 值为:0~3,共4行数据;

step2:根据“,”逗号来拆分字符串,此处利用 substring_index(str, delim, count) 函数,最后把结果赋值给 num 字段。

涉及的代码片段:

substring_index(substring_index('7654,7698,7782,7788',',',help_topic_id+1),',',-1) as num 

第一步:

以”,”逗号为分隔符,根据 help_topic_id 的值来截取第n+1个分隔符之前所有的字符串。 (此处 n+1 是因为help_topic_id 是从0开始算起,而此处需从第1个分隔符开始获取。)

substring_index('7654,7698,7782,7788',',',help_topic_id+1)

eg: 
当 help_topic_id = 0时,获取到的字符串 = 7654 
当 help_topic_id = 1时,获取到的字符串 = 7654,7698 …(以此类推)

第二步:

以”,”逗号为分隔符,截取倒数第1个分隔符之后的所有字符串。

substring_index(substring_index('7654,7698,7782,7788',',',help_topic_id+1),',',-1)

eg: 
根据第一步,当 help_topic_id = 0时,获取到的字符串 = 7654,此时第二步截取的字符串 = 7654 
根据第一步,当 help_topic_id = 1时,获取到的字符串 = 7654,7698,此时第二步截取的字符串 = 7698 
…(以此类推)

最终成功实现了以下效果 ~

扩展:判断外部值是否在 num列值中

5.1】find_in_set

如果匹配到了会得出1;如下图

实际业务中,我们只需要 where find_in_set(id,ids)>0

就可以判断出;id列,是否在 ids列中出现过;做表连接的时候,也可以这样;

5.2】instr

我们可以看出,instr是找出 参数2=》也就是上图中的 ‘123’ 在参数1=》也就是上图中的 ‘321,123,555,12345’ 中最开始出现的位置;

所以我们也只需要 where find_in_set(ids,id)>0 ,就可以判断出 id 在 ids中出现过;

但这有一个问题,如果逗号分隔开的字符串,包含我们查找的字符串,也会显示出来,这就不符合我们 根据分隔符 , 判断 查找字符串id 是否出现在 ids 中;

如下:

我们本来想查以逗号为分隔的完全匹配,但是12345包含了 123 所以查出来的结果也是>0的,这不对;

所以我们为了避免这种情况,可以加上分隔符;然后再用 字符串+分隔符作为 查找的字符串 来 匹配;

我们被查找的字符串 ids 中 再加上一个正常的 123, 再查看,如下图:确实是对的

文章参考

https://blog.csdn.net/pjymyself/article/details/81668157

https://www.cnblogs.com/gered/p/10797012.html#_label1

到此这篇关于mysql字符串拆分实现split功能(字段分割转列)的文章就介绍到这了,更多相关mysql字符串拆分内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

(0)
上一篇 2022年7月1日
下一篇 2022年7月1日

相关推荐