在 MySQL 数据库中快速搜索字符串?
mysqlmysqli database
使用 FULLTEXT 搜索快速搜索字符串。让我们首先创建一个表 −
mysql> create table DemoTable1554 -> ( -> Title text -> ); Query OK, 0 rows affected (0.63 sec)
这是创建全文搜索的查询 −
mysql> create fulltext index faster_title on DemoTable1554(Title); Query OK, 0 rows affected, 1 warning (7.09 sec) Records: 0 Duplicates: 0 Warnings: 1
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable1554 values('John is working on MySQL database'); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable1554 values('Adam Smith is working on Java language'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1554 values('John smith is working on Python Language'); Query OK, 1 row affected (0.17 sec)
使用 select 语句显示表中的所有记录 −
mysql> select * from DemoTable1554;
这将产生以下输出 −
+------------------------------------------+ | Title | +------------------------------------------+ | John is working on MySQL database | | Adam Smith is working on Java language | | John smith is working on Python Language | +------------------------------------------+ 3 rows in set (0.00 sec)
以下是在 MySQL 数据库中快速搜索字符串的查询。这里,我们搜索字符串"language" −
mysql> select * from DemoTable1554 where match(Title) against('language' in boolean mode);
这将产生以下输出 −
+------------------------------------------+ | Title | +------------------------------------------+ | Adam Smith is working on Java language | | John smith is working on Python Language | +------------------------------------------+ 2 rows in set (0.00 sec)