Mysql 字符串拆分 OR 一行转多行

 

Mysql 字符串拆分 OR 一行转多行

 

需要了解的的几个mysql 函数:

A.substring_index():字符串截取 substring_index(str,delim,count)

  str:要处理的字符串

      delim:分隔符

      count:计数

B.length():字符长度函数
C.replace():替换函数

不说了,直接看代码:

SELECT
    substring_index(substring_index(    a.chain,'_',b.help_topic_id + 1    ),    '_' ,- 1    ) AS ID
FROM
    (select '1_11_1223_1242' as chain) a  
JOIN mysql.help_topic b ON b.help_topic_id <
(length(a.chain) - length( replace(a.chain, '_', '')  ) + 1)

结果👇:

 首先如果有疑问,可以在数据库进行测试,一步一步进行推断执行,你就会明白了,实践是最好的学习成长捷径。

 

posted @ 2020-05-02 14:25  小花田半亩  阅读(4682)  评论(0编辑  收藏  举报