计算mysql中某个字段某字符出现的次数,case when 和 截取字符的用法
select LENGTH(type) - LENGTH(replace(type,'_','')) as counts from sa_log_olap where type like 'XX_XXXX_%'
CASE WHEN LENGTH(type) - LENGTH(replace(type,'_',''))=2 THEN 'parent'
WHEN LENGTH(type) - LENGTH(replace(type,'_',''))=3 and SUBSTRING_INDEX(replace(type,concat(concat('XX_',#{name}),'_'),''),'_',1)!=''
THEN SUBSTRING_INDEX(replace(type,concat(concat('XX_',#{name}),'_'),''),'_',1)
WHEN LENGTH(type) - LENGTH(replace(type,'_',''))=3 and SUBSTRING_INDEX(replace(type,concat(concat('XX_',#{name}),'_'),''),'_',1)=''
THEN 'parent'