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