现有如下需求:需要将字符串
1,2,3,4,5,6,7
拆分成
1
2
3
4
5
6
7
分析:
为了完成上述功能,在mysql中提供了一些字符串操作的函数,其中SUBSTRING_INDEX(str, delim, count)
str: 要处理的字符串
delim: 分割符
count: 计数 如果为正数,则从左开始数,如果为负数,则从右开始数
例:
str = 'www.baidu.com';
SELECT substring_index('www.baidu.com','.', 1); #www
SELECT substring_index('www.baidu.com','.', 2); #www.baidu
SELECT substring_index('www.baidu.com','.', -1); #com
SELECT substring_index('www.baidu.com','.', -2); #baidu.com
SELECT substring_index(substring_index('www.baidu.com','.', -2), '.', 1); #baidu