[Project Combat] MySQL query calculates the number of a character in a field

1. Background

There is a field ancestors in the table sys_dept, and the value of ancestors contains "," (comma)
Now it is necessary to calculate the number of occurrences of the string "," (comma) in ancestors

Two, the solution

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

Guess you like

Origin blog.csdn.net/wstever/article/details/131445615#comments_27828173