為此,請使用 MySQL 中的 CHAR_LENGTH() 函數(shù)。讓我們首先創(chuàng)建一個表 –
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Subject longtext ); Query OK, 0 rows affected (1.17 sec)
登錄后復制
現(xiàn)在您可以使用插入命令在表中插入一些記錄 –
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 查詢來查找字符串計數(shù)的查詢。我們在這里檢查 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 的字符串計數(shù)?的詳細內容,更多請關注www.92cms.cn其它相關文章!