我如何才能通过 MySQL 统计特定单词在一个列中出现的次数?


为此,可以使用 COUNT() 函数。我们先创建一个表 -

mysql> create table DemoTable
   -> (
   -> EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> EmployeeName varchar(100)
   -> );
Query OK, 0 rows affected (0.62 sec)

使用 insert 命令在表中插入一些记录 -

mysql> insert into DemoTable(EmployeeName) values('John');
Query OK, 1 row affected (0.21 sec)

mysql> insert into DemoTable(EmployeeName) values('Carol');
Query OK, 1 row affected (0.17 sec)

mysql> insert into DemoTable(EmployeeName) values('David');
Query OK, 1 row affected (0.14 sec)

mysql> insert into DemoTable(EmployeeName) values('Carol');
Query OK, 1 row affected (0.14 sec)

mysql> insert into DemoTable(EmployeeName) values('Carol');
Query OK, 1 row affected (0.29 sec)

mysql> insert into DemoTable(EmployeeName) values('Bob');
Query OK, 1 row affected (0.22 sec)

mysql> insert into DemoTable(EmployeeName) values('Sam');
Query OK, 1 row affected (0.12 sec)

使用 select 语句显示表中的所有记录 -

mysql> select *from DemoTable;

输出

这将产生以下输出 -

+------------+--------------+
| EmployeeId | EmployeeName |
+------------+--------------+
|          1 | John         |
|          2 | Carol        |
|          3 | David        |
|          4 | Carol        |
|          5 | Carol        |
|          6 | Bob          |
|          7 | Sam          |
+------------+--------------+
7 rows in set (0.00 sec)

以下是在 MySQL 中获取特定单词出现次数的查询。在这里,我们查找员工 “Carol” 的出现次数 -

mysql> select EmployeeName,count(EmployeeId) as Occurrence from DemoTable where
EmployeeName='Carol';

输出

这将产生以下输出 -

+--------------+------------+
| EmployeeName | Occurrence |
+--------------+------------+
| Carol        | 3          |
+--------------+------------+
1 row in set (0.05 sec)

更新于: 30-06-2020

1000+ 次浏览

开启您的职业生涯

完成课程即可获得认证

开始
广告