计算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'

posted @ 2019-01-04 16:38  小魔魔  阅读(1406)  评论(0编辑  收藏  举报