Mysql中字段分割字符串( 一行转多行 )

t_customers 表中的一条记录:

需要的结果:

sql实现

SELECT
    a.id,
    a.username,
    SUBSTRING_INDEX( SUBSTRING_INDEX( a.operid, ';', b.help_topic_id + 1 ), ';',- 1 ) AS ids 
FROM
    `t_customers` AS a
    JOIN mysql.help_topic AS b ON b.help_topic_id < ( length( a.operid ) - length( REPLACE ( a.operid, ';', '' ) ) + 1 ); 

如mysql.help_topic: help_topic_id 共有504个数值 它们是mysql内部的连续数列表,连续数列的最大值一定要大于符合分割的值的个数。

------------------------------------------------------------------------------------------------------------------------------------

mysql 字段截取函数:

1、left(字段名,index) 从左边开始第index开始截取

2、right(字段名,index)从右边开始第index开始截取

3、substring(字段名,index)当index>0从左边开始截取直到结束  当index<0从右边开始截取直到结束  当index=0返回空

4、substring(字段名,index,len)从index开始,截取len长度

5、substring_index(字段名,str,count),str是截取的字段 count是从哪里开始截取(0从左边第0个开始,-1从右边第一个开始)

posted @ 2019-08-08 11:47  爪哇搬砖  阅读(21246)  评论(2编辑  收藏  举报