需求:将单行逗号拼接数据进行切割,换成多行数据
1. 数据库结构数据图
2.SQL
SELECT
SUBSTRING_INDEX( SUBSTRING_INDEX( pn.plan_num, ',', help_topic_id + 1 ), ',',- 1 ) AS plannum
FROM
( SELECT plan_num FROM te_deepen_plan WHERE plan_id = 19 ) pn
JOIN mysql.help_topic b
WHERE
b.help_topic_id < LENGTH( pn.plan_num )- LENGTH(
REPLACE ( pn.plan_num, ',', '' ))+ 1
单查询数据截图
执行后数据截图
3.substring_index(string,sep,num)切割函数
string:用于截取目标字符串的字符串。可为字段,表达式等。
sep:分隔符,string存在且用于分割的字符,比如“,”、“.”等。
num:序号,为非0整数。若为整数则表示从左到右数,若为负数则从右到左数。比如“www.mysql.com”截取字符‘www’,分割符为“.”,从左到右序号为1,即substring_index(“www.mysql.com”,‘.’,1);若从右开始获取“com”则为序号为-1即substring_index(“www.mysql.com”,‘.’,-1)
4.LENGTH( pn.plan_num ) 字符长度函数
查看某字符串的长度(pn.plan_num)
5. replace()替换函数
语法: REPLACE ( string_expression , string_pattern , string_replacement)
参数:
string_expression:字符串表达式
string_pattern:想要查找的子字符串
string_replacement:想要替换成的子字符串