`

mysql把一字段拆分为多行

阅读更多

一:数据库

二:sql语句

select a.house_no as '房子',substring_index(substring_index(a.name,',',b.help_topic_id+1),',',-1) as '拥有者' 
from  aa a
join   mysql.help_topic b on b.help_topic_id < (length(a.name) - length(replace(a.name,',',''))+1);

三:查询结果

from https://blog.csdn.net/m0_37934074/article/details/78784172

 

MySQL  substring_index函数

substring_index(str,delim,count)

      str:要处理的字符串

      delim:分隔符

      count:计数

例子:str=www.wikibt.com

      substring_index(str,'.',1)

      结果是:www

      substring_index(str,'.',2)

      结果是:www.wikibt

      也就是说,如果count是正数,那么就是从左往右数,第N个分隔符的左边的全部内容

      相反,如果是负数,那么就是从右边开始数,第N个分隔符右边的所有内容,如:

      substring_index(str,'.',-2)

      结果为:wikibt.com

     有人会问,如果我要中间的的wikibt怎么办?

     很简单的,两个方向:

      从右数第二个分隔符的右边全部,再从左数的第一个分隔符的左边:

  substring_index(substring_index(str,'.',-2),'.',1);

 

from https://www.cnblogs.com/cyfblogs/p/11406677.html

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics