Set search feature in MySQL for full text searching

MySQLMySQLi Database

For this, use FULLTEXT search index. The Full-text searching is performed using MATCH() ... AGAINST syntax.

Let us first create a table −

mysql> create table DemoTable1542
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> Name varchar(20)
   -> );
Query OK, 0 rows affected (2.16 sec)

Following is the query to create fulltext search index −

mysql> create fulltext index search_name on DemoTable1542(Name);
Query OK, 0 rows affected, 1 warning (10.51 sec)
Records: 0  Duplicates: 0  Warnings: 1

Insert some records in the table using insert command −

mysql> insert into DemoTable1542(Name) values('John Doe');
Query OK, 1 row affected (0.31 sec)
mysql> insert into DemoTable1542(Name) values('Chris Brown');
Query OK, 1 row affected (0.29 sec)
mysql> insert into DemoTable1542(Name) values('John Smith');
Query OK, 1 row affected (0.46 sec)
mysql> insert into DemoTable1542(Name) values('Adam Smith');
Query OK, 1 row affected (4.71 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1542;

This will produce the following output −

+----+-------------+
| Id | Name        |
+----+-------------+
|  1 | John Doe    |
|  2 | Chris Brown |
|  3 | John Smith  |
|  4 | Adam Smith  |
+----+-------------+
4 rows in set (0.04 sec)

Here is the query to use match…against for fulltext search −

mysql> select * from DemoTable1542 where match(Name) against('John' in boolean mode);

This will produce the following output −

+----+------------+
| Id | Name       |
+----+------------+
|  1 | John Doe   |
|  3 | John Smith |
+----+------------+
2 rows in set (0.03 sec)
raja
Published on 12-Dec-2019 06:01:36
Advertisements