首页 > 代码库 > 6.14 提取第n个分割的子串

6.14 提取第n个分割的子串

问题:从字符串中提取出一个指定的、由分割符隔开的子字符串。
create view v as
select ‘mo,larry,curly‘ as name
from t1
union all
select ‘tina,gina,jaunita,regina,leena‘ as name from t1;

select * from v;
+--------------------------------+
| name                           |
+--------------------------------+
| mo,larry,curly                 |
| tina,gina,jaunita,regina,leena |
+--------------------------------+

 

解决方案:

select name
from (
select iter.pos,
substring_index(
substring_index(src.name,‘,‘,iter.pos),‘,‘,-1) name
from v src,
(select id pos from t10) iter
where iter.pos <=
length(src.name)-length(replace(src.name,‘,‘,‘‘))
)x
where pos =2;

+-------+
| name  |
+-------+
| larry |
| gina  |
+-------+

6.14 提取第n个分割的子串