For this, you can use COUNT() function. Let us first create a table −
mysql> create table DemoTable -> ( -> EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> EmployeeName varchar(100) -> ); Query OK, 0 rows affected (0.62 sec)
Insert some records in the table using insert command −
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)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+------------+--------------+ | EmployeeId | EmployeeName | +------------+--------------+ | 1 | John | | 2 | Carol | | 3 | David | | 4 | Carol | | 5 | Carol | | 6 | Bob | | 7 | Sam | +------------+--------------+ 7 rows in set (0.00 sec)
Following is the query to get the number of times a specific word appears in MySQL. Here, we are finding the occurrences of Employee “Carol” −
mysql> select EmployeeName,count(EmployeeId) as Occurrence from DemoTable where EmployeeName='Carol';
Output
This will produce the following output −
+--------------+------------+ | EmployeeName | Occurrence | +--------------+------------+ | Carol | 3 | +--------------+------------+ 1 row in set (0.05 sec)