为此,请使用 mysql 中的 char_length() 函数。让我们首先创建一个表 -
mysql> create table demotable ( id int not null auto_increment primary key, subject longtext );query ok, 0 rows affected (1.17 sec)
现在您可以使用插入命令在表中插入一些记录 -
mysql> insert into demotable(subject) values('mysql,mongodb');query ok, 1 row affected (0.20 sec)mysql> insert into demotable(subject) values('mysql,mongodb');query ok, 1 row affected (0.17 sec)mysql> insert into demotable(subject) values('mongodb');query ok, 1 row affected (0.13 sec)mysql> insert into demotable(subject) values('mysql');query ok, 1 row affected (0.15 sec)display all records from the table using select statement :mysql> select *from demotable;
输出+----+---------------+| id | subject |+----+---------------+| 1 | mysql,mongodb || 2 | mysql,mongodb || 3 | mongodb || 4 | mysql |+----+---------------+4 rows in set (0.00 sec)
下面是通过对列中特定 id 的 mysql 查询来查找字符串计数的查询。我们在这里检查 id 1 -
mysql> select id,char_length(subject) - char_length(replace((subject), ',', ''))+1 as freqsubject from demotable where char_length(subject) > 0 and id = 1;
输出+----+-------------+| id | freqsubject |+----+-------------+| 1 | 2 |+----+-------------+1 row in set (0.02 sec)
以上就是如何使用 mysql 查询查找列中特定 id 的字符串计数?的详细内容。
