以下是语法-
select length(yourColumnName) - length(replace(yourColumnName, ',', '')) as anyAliasName from yourTableName;
让我们首先创建一个表-
create table DemoTable1510 -> ( -> Value varchar(50) -> );
使用插入命令在表中插入一些记录-
insert into DemoTable1510 values('20,35'); insert into DemoTable1510 values('45,67,89'); insert into DemoTable1510 values('90,97,101,190');
使用select语句显示表中的所有记录-
select * from DemoTable1510;
这将产生以下输出-
+---------------+ | Value | +---------------+ | 20,35 | | 45,67,89 | | 90,97,101,190 | +---------------+ 3 rows in set (0.00 sec)
这是从字段值中计数逗号的查询-
select length(Value) - length(replace(Value, ',', '')) as NumberOfComma from DemoTable1510;
这将产生以下输出-
+---------------+ | NumberOfComma | +---------------+ | 1 | | 2 | | 3 | +---------------+ 3 rows in set (0.00 sec)