我如何才能透過 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+ 次瀏覽

開啟您的職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.