【项目实战】MySQL查询计算某个字符在某个字段中的数量

一、背景说明

表sys_dept中有个字段 ancestors,ancestors的值是含有“,”(逗号)
现在需要计算“,”(逗号)这个字符串在ancestors中出现的数量

二、解决方案

SELECT
	dept_id,
	dept_name,
	ancestors,
	round((
		CHAR_LENGTH(ancestors) - CHAR_LENGTH(REPLACE(ancestors, ',', ''

猜你喜欢

转载自blog.csdn.net/wstever/article/details/131445615#comments_27828173
今日推荐