如何在 MySQL 数据库中快速搜索字符串?


使用 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

使用插入命令向表中插入一些记录 -

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)

更新时间:12-12-2019

414 次浏览

助力你的职业生涯

通过完成课程获得认证

开始学习
广告