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, ',', ''