各位用户为了找寻关于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、 举例
(一)从第2个字符开始获取字符串'p1111'
? 1substring
(
'p1111'
,2)
(二)从倒数第2个字符开始获取字符串'p1111'
? 1substring
(
'p1111'
,-2)
二、从左边开始截取字符串:left(str,len)
1、参数说明
2、举例
(一) 获取字符串'p1111'最左边的3个字符
? 1left
(
'p1111'
,3)
tip:
有从左边开始截取字符串的函数 left(str,len),那当然也有从右边开始截取字符串的 right(str,len), 左右两个函数原理一样,区别在于是从左边开始还是右边开始截取字符串。
sql解析
? 1select
left
(
substring
(
'p1111'
,help_topic_id+1),1)
as
num
from
mysql.help_topic
where
help_topic_id < length(
'p1111'
);
此处同样利用 mysql 库的 help_topic 表的 help_topic_id 来作为变量,因为 help_topic_id 是自增的,当然也可以用其他表的自增字段辅助。
可参考 mysql——字符串拆分(一)
实现步骤
step1:
获取字符串 ‘p1111' 的长度,利用 help_topic_id 来动态模拟对字符串 ‘p1111' 的遍历
? 1help_topic_id < length(
'p1111'
)
step2:
利用 substring(str,pos) 函数 和 help_topic_id 对字符串 ‘p1111' 进行截取。(此处“help_topic_id+1”的原因是 help_topic_id 是从0开始的,而 substring 函数需从第1个位置开始截取字符串)
? 1substring
(
'p1111'
,help_topic_id+1)
eg:
当 help_topic_id = 0 时,获取到的字符串 = p1111
当 help_topic_id = 1 时,获取到的字符串 = 1111
…(以此类推)
step3:在实现第2步的基础上,结合 left(str,len) 函数来获取第2步中最左边的第1个字符
? 1left
(
substring
(
'p1111'
,help_topic_id+1),1)
eg:
根据第2步,当 help_topic_id = 0 时,获取到的字符串 = p1111,此时第3步获取的字符串 = p
根据第2步,当 help_topic_id = 1 时,获取到的字符串 = 1111,此时第3步获取的字符串 = 1
…(以此类推)
最终成功实现以下效果
注:含分隔符的字符串拆分可参考 mysql——字符串拆分(含分隔符的字符串截取)
补充:mysql将查出的字符串拆分_mysql拆分字符串查询
我就废话不多说了,大家还是直接看代码吧~
? 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59delimiter $$
drop
function
if exists `tms1`.`getclassname` $$
create
function
`getclassname`(f_string
varchar
(15000))
returns
varchar
(15000)
begin
/* 判断字符串包含,的第一个位置*/
declare
the_cnt
int
(15)
default
1;
/* 班级编号*/
declare
classid
varchar
(20)
default
''
;
/* 返回的班级名称*/
declare
result
varchar
(15000)
default
null
;
/* 班级名称*/
declare
classname
varchar
(50)
default
''
;
/* 字符串包含,的第一个位置*/
set
the_cnt = locate(
','
,f_string);
/* 判断字符串包含,的第一个位置是否存在*/
while (the_cnt >= 0) do
/* ,位置不存在的场合*/
if the_cnt = 0
then
/* 班级编号的设置*/
set
classid = f_string;
else
/* 字符串中获得班级编号*/
set
classid = substring_index(substring_index(f_string,
','
, 1),
','
, -1);
end
if ;
/* 根据班级编号获得班级名称*/
select
(
select
name
from
class
where
id = classid)
into
classname;
/* 返回班级编号的字符串为空的场合*/
if result
is
null
then
/* 根据编号没有查询到班级名称的场合*/
if classname
is
null
then
/* 设置班级名称为空*/
set
classname =
' '
;
end
if;
/* 班级名称追加到字符串*/
set
result = classname;
else
/* 根据编号没有查询到班级名称的场合*/
if classname
is
null
then
/* 设置班级名称为空*/
set
classname =
' '
;
end
if;
/* 班级名称追加到字符串*/
set
result = concat(result,
','
,classname);
end
if;
/* ,位置不存在的场合*/
if the_cnt = 0
then
/* 返回结果集*/
return
result;
end
if;
/* 截取传入的字符串*/
set
f_string =
right
(f_string,length(f_string) - the_cnt);
/* 字符串包含,的第一个位置*/
set
the_cnt = locate(
','
,f_string);
/* 结束遍历*/
end
while;
/* 返回结果集*/
return
result;
end
$$
delimiter ;
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。
原文链接:https://blog.csdn.net/pjymyself/article/details/81669928