现有strings表如下:
- id指序列号;
- string列中存放的是字符串,且字符串中仅包含数字、字母和逗号类型的字符。
id | string |
1 | 10,A,B,C,D |
2 | A,B,C,D,E,F |
3 | A,11,B,C,D,E,G |
请你统计每个字符串中逗号出现的次数cnt。
以上例子的输出结果如下:
id | cnt |
1 | 4 |
2 | 5 |
3 | 6 |
id | string |
1 | 10,A,B,C,D |
2 | A,B,C,D,E,F |
3 | A,11,B,C,D,E,G |
id | cnt |
1 | 4 |
2 | 5 |
3 | 6 |
drop table if exists strings; CREATE TABLE strings( id int(5) NOT NULL PRIMARY KEY, string varchar(45) NOT NULL ); insert into strings values (1, '10,A,B'), (2, 'A,B,C,D'), (3, 'A,11,B,C,D,E');
1|2 2|3 3|5
select id, length(string)-length(replace(string,",","")) as cnt from strings -- 逗号个数 = 总长度 - 无逗号长度
select id, length(string) - length(replace(string,',','')) cnt from strings
select id,length(string)-length(replace(string,',','')) from strings