help_topic表,以字符拆分,一行转多行

转自:https://www.cnblogs.com/likaixin/p/11271756.html

help_topic表是数据库mysql下的一个表

help_topic表,以字符拆分,一行转多行_第1张图片

SUBSTRING_INDEX(s, delimiter, number)    

返回从字符串 s 的第 number 个出现的分隔符 delimiter 之后的子串。

1、如果 number 是正数,返回第 number 个字符左边的字符串。

SELECT SUBSTRING_INDEX('a*b*c*d*e','*',3);    ---- a*b*c

1、如果 number 是负数,返回第(number 的绝对值(从右边数))个字符右边的字符串。

SELECT SUBSTRING_INDEX('a*b*c','*',-1);    ---- c
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX('a*b*c*d*e','*',3),'*',-1);    ---- c
示例: 

help_topic表,以字符拆分,一行转多行_第2张图片

select a.id, a.username, 
substring_index(substring_index(a.course, '|', b.help_topic_id + 1), '|', -1) course  
from student a JOIN mysql.help_topic b 
ON b.help_topic_id < (length(a.course) - length(REPLACE(a.course, '|', '')) + 1);

help_topic表,以字符拆分,一行转多行_第3张图片

你可能感兴趣的:(sql,mysql)