在MySQL中设置搜索功能以进行全文搜索
为此,请使用FULLTEXT搜索索引。使用MATCH()
...AGAINST语法执行全文搜索。
让我们首先创建一个表-
mysql> create table DemoTable1542 -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(20) -> );
以下是创建全文搜索索引的查询-
mysql> create fulltext index search_name on DemoTable1542(Name); Records: 0 Duplicates: 0 Warnings: 1
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1542(Name) values('John Doe'); mysql> insert into DemoTable1542(Name) values('Chris Brown'); mysql> insert into DemoTable1542(Name) values('John Smith'); mysql> insert into DemoTable1542(Name) values('Adam Smith');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1542;
这将产生以下输出-
+----+-------------+ | Id | Name | +----+-------------+ | 1 | John Doe | | 2 | Chris Brown | | 3 | John Smith | | 4 | Adam Smith | +----+-------------+ 4 rows in set (0.04 sec)
这是针对全文搜索使用match...的查询-
mysql> select * from DemoTable1542 where match(Name) against('John' in boolean mode);
这将产生以下输出-
+----+------------+ | Id | Name | +----+------------+ | 1 | John Doe | | 3 | John Smith | +----+------------+ 2 rows in set (0.03 sec)