SQL | substring_index 字符串拆分

1、语法

  substring_index(str,delim,count)

      str:要处理的字符串

      delim:分隔符

      count:计数:如果count是正数,那么就是从左往右数,第N个分隔符的左边的全部内容;若为负数,则从右往左数

2、案例

2.1. 简单便于理解的例子

str='www.cnblogs.com'

substring_index(str,'.',1)

运行结果:www

substring_index(str,'.',1)

运行结果:www.cnblogs

2.2. 实际工作中的使用

str='{"id":"122345","Name":"Jack","shopId":"5820721","nameId":"6654321","version":"three","productName":"哈哈哈"}'

要取name对应的内容:

substring_index(substring_index(str,'"Name":"',-1),'","',1)

解析:

第一步:substring_index(str,'"Name":"',-1) 取出 Jack","shopId":"5820721","nameId":"6654321","version":"three","productName":"哇哈哈哈"}

第二步取出 Jack

 

posted @ 2021-09-15 11:03  机器快点学习  阅读(472)  评论(0编辑  收藏  举报