SQL245. 查找字符串中逗号出现的次数
描述
id | string |
1 2 3 | 10,A,B,C,D A,B,C,D,E,F A,11,B,C,D,E,G |
id | cnt |
1 2 3 | 4 5 6 |
示例1
输入:
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
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3352KB, 提交时间: 2022-01-27
select id, length(string)-length(replace(string,",","")) from strings
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3364KB, 提交时间: 2022-01-24
select id,length(string) - length(replace(string,',',''))as cnt from strings
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3388KB, 提交时间: 2022-01-26
SELECT ID,LENGTH(string)-LENGTH(REPLACE(string,',','')) cnt FROM strings
Sqlite 解法, 执行用时: 11ms, 内存消耗: 3356KB, 提交时间: 2022-02-05
select id,( length(String)-length(replace(string,",",""))) as cnt from strings
Sqlite 解法, 执行用时: 11ms, 内存消耗: 3360KB, 提交时间: 2021-12-10
select id,length(string)-length(replace(string,",","")) from strings