SQL字符串按指定的字符拆分

直接上SQL:

SELECT 
   substring_index( substring_index( a.sound_urls, ',', b.help_topic_id + 1 ), ',',- 1 ) surl
FROM
    tab_test a
    JOIN mysql.help_topic b ON b.help_topic_id < (( length( a.sound_urls ) - length( REPLACE ( a.sound_urls, ',', '' ) ))/length(',') + 1 ) 

知识点:

1、substring_index(str, delim, count) 方法

str:需要拆分的字符串;

delim:分隔符,根据此字符来拆分字符串;

count:当 count 为正数,取第 n 个分隔符之前的所有字符; 当 count 为负数,取倒数第 n 个分隔符之后的所有字符

2、mysql.help_topic表中help_topic_id从0自增的特性

3、replace(str, beReplaceStr, replaceStr)

4、length(str)

SQL分析:

substring_index( substring_index( a.sound_urls, ',', b.help_topic_id + 1 ), ',',-1 )

根据help_topic_id的变化来分拆字符,每次都取substring_index( a.sound_urls, ',', b.help_topic_id + 1 )中的最后一个字符

JOIN mysql.help_topic b ON b.help_topic_id < ( length( a.sound_urls ) - length( REPLACE ( a.sound_urls, ',', '' ) ) + 1 ) 

利用( length( a.sound_urls ) - length( REPLACE ( a.sound_urls, ',', '' ) ) + 1 )确定循环拆分的次数,help_topic_id是从0开始

 

  • 2
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值