mysql SQL欄位拆分
阿新 • • 發佈:2018-12-18
現有如下需求:需要將字串
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