在进行日志分析时,需要从海量日志数据中解析想要的数据,经常需要用到字符串截取操作,比如 '....10012a230a3060....',我们想要获取第一个'a'字符后的第一个字符,用mysql实现如下:
-- 创建测试数据,临时表
drop table if exists t_k1;
create temporary table t_k1 as
select '10012abcd230abcd3060' str
union all
select '100abcd200' str;
-- 通过 substring 和 locate 函数进行定位和拆分
SELECT
substring( str, LOCATE( 'abcd', str )+ length( 'abcd' ), 1 ) new_str
FROM
t_k1;