各位用户为了找寻关于MySQL 字符串拆分操作(含分隔符的字符串截取)的资料费劲了很多周折。这里教程网为您整理了关于MySQL 字符串拆分操作(含分隔符的字符串截取)的相关资料,仅供查阅,以下为您介绍关于MySQL 字符串拆分操作(含分隔符的字符串截取)的详细内容
无分隔符的字符串截取
题目要求
数据库中字段值:
实现效果:需要将一行数据变成多行
实现的sql
? 1select
left
(
substring
(
'p1111'
,help_topic_id+1),1)
as
num
from
mysql.help_topic
where
help_topic_id < length(
'p1111'
);
涉及的知识点
一、字符串截取:substring(str,pos)
1、参数说明
2、 举例
(1)获取第2个以“,”逗号为分隔符之前的所有字符。
? 1substring_index(
'7654,7698,7782,7788'
,
','
,2)
(2)获取倒数第2个以“,”逗号分隔符之后的所有字符
? 1substring_index(
'7654,7698,7782,7788'
,
','
,-2)
二、替换函数:replace( str, from_str, to_str)
1、参数解说
2、 举例
(1)将分隔符“,”逗号替换为“”空。
? 1replace
(
'7654,7698,7782,7788'
,
','
,
''
)
三、获取字符串长度:length( str )
1、参数解说
2、举例
(1)获取 ‘7654,7698,7782,7788' 字符串的长度
? 1length(
'7654,7698,7782,7788'
)
实现的sql解析
? 1 2 3 4 5 6select
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 表:
实现步骤:
step1:首先获取最后需被拆分成多少个字符串,利用 help_topic_id 来模拟遍历 第n个字符串。
涉及的代码片段:
? 1help_topic_id < length(
'7654,7698,7782,7788'
)-length(
replace
(
'7654,7698,7782,7788'
,
','
,
''
))+1
step2:根据“,”逗号来拆分字符串,此处利用 substring_index(str, delim, count) 函数,最后把结果赋值给 num 字段。
涉及的代码片段:
? 1substring_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个分隔符开始获取。)
? 1substring_index(
'7654,7698,7782,7788'
,
','
,help_topic_id+1)
eg:
当 help_topic_id = 0时,获取到的字符串 = 7654
当 help_topic_id = 1时,获取到的字符串 = 7654,7698
…(以此类推)
第二步:
以”,”逗号为分隔符,截取倒数第1个分隔符之后的所有字符串。
? 1substring_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
…(以此类推)
最终成功实现了以下效果 ~
注:不含分隔符的字符串拆分可参考 mysql——字符串拆分(无分隔符的字符串截取)
补充:mysql字段分隔符拆分_mysql里实现类似split的分割字符串的函数
下边的函数,实现了象数组一样去处理字符串。
一、用临时表作为数组
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
create
function
f_split(@c
varchar
(2000),@split
varchar
(2))
returns
@t
table
(col
varchar
(20))
as
begin
while(charindex(@split,@c)<>0)
begin
insert
@t(col)
values
(
substring
(@c,1,charindex(@split,@c)-1))
set
@c = stuff(@c,@c),
''
)
end
insert
@t(col)
values
(@c)
return
end
go
select
*
from
dbo.f_split(
'dfkd,dfdkdf,dfdkf,dffjk'
,
','
)
drop
function
f_split
col
--------------------
dfkd
dfdkdf
dfdkf
dffjk
(所影响的行数为 4 行)
二、按指定符号分割字符串
返回分割后的元素个数,方法很简单,就是看字符串中存在多少个分隔符号,然后再加一,就是要求的结果。
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22create
function
get_strarraylength
(
@str
varchar
(1024),
--要分割的字符串
@split
varchar
(10)
--分隔符号
)
returns
int
as
begin
declare
@location
int
declare
@start
int
declare
@length
int
set
@str=ltrim(rtrim(@str))
set
@location=charindex(@split,@str)
set
@length=1
while @location<>0
begin
set
@start=@location+1
set
@location=charindex(@split,@str,@start)
set
@length=@length+1
end
return
@length
end
调用示例:
? 1select
dbo.get_strarraylength(
'78,2,3'
,')
返回值:4
三、按指定符号分割字符串
返回分割后指定索引的第几个元素,象数组一样方便
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28create
function
get_strarraystrofindex
(
@str
varchar
(1024),
--要分割的字符串
@split
varchar
(10),
--分隔符号
@
index
int
--取第几个元素
)
returns
varchar
(1024)
as
begin
declare
@location
int
declare
@start
int
declare
@
next
int
declare
@seed
int
set
@str=ltrim(rtrim(@str))
set
@start=1
set
@
next
=1
set
@seed=len(@split)
set
@location=charindex(@split,@str)
while @location<>0
and
@
index
>@
next
begin
set
@start=@location+@seed
set
@location=charindex(@split,@start)
set
@
next
=@
next
+1
end
if @location =0
select
@location =len(@str)+1
--这儿存在两种情况:1、字符串不存在分隔符号 2、字符串中存在分隔符号,跳出while循环后,@location为0,那默认为字符串后边有一个分隔符号。
return
substring
(@str,@start,@location-@start)
end
调用示例:
? 1select
dbo.get_strarraystrofindex(
'8,9,4'
,2)
返回值:9
四、结合上边两个函数,象数组一样遍历字符串中的元素
? 1 2 3 4 5 6 7 8 9
declare
@str
varchar
(50)
set
@str=
'1,3,4,5'
declare
@
next
int
set
@
next
=1
while @
next
<=dbo.get_strarraylength(@str,')
begin
print dbo.get_strarraystrofindex(@str,@
next
)
set
@
next
=@
next
+1
end
调用结果:
1
2
3
4
5
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。
原文链接:https://blog.csdn.net/pjymyself/article/details/81668157