MySQL - MATCH 运算符
MySQL MATCH 运算符用于在指定的列列表中搜索特定字符串,并在匹配的情况下返回该列的内容。
此运算符使用全文索引执行搜索操作。全文索引是一种特殊类型的索引,可以高效地对大型数据集进行基于文本的搜索。
创建包含全文索引的表时,MySQL 会为指定列中包含的单词构建索引。然后,MATCH 运算符会使用此索引快速搜索符合搜索条件的字符串。
语法
以下是 MySQL Match 运算符的语法:
MATCH (col1,col2,...) AGAINST (expr [search_modifier]) search_modifier: { IN NATURAL LANGUAGE MODE | IN NATURAL LANGUAGE MODE WITH QUERY EXPANSION | IN BOOLEAN MODE | WITH QUERY EXPANSION }
其中,
(col1, col2, ...) 是要在其中进行搜索的列的名称列表,以逗号分隔。
expr 是表示要作为参数进行搜索的字符串的字符串值。
示例
假设我们使用 CREATE 语句创建了表"Tutorials_table",并在其中插入了除"Contents"列之外的记录 -
CREATE TABLE Tutorials_table ( ID INT, Title VARCHAR(20), Contents LONGTEXT );
现在,让我们使用 INSERT 语句将记录插入其中 -
INSERT INTO Tutorials_table (ID, Title) VALUES (1, 'Java'), (2, 'JavaFX'), (3, 'Coffee Script'), (4, 'OpenCV');
假设我们在 MySQL 数据目录中有 4 个文本文件,即 C:\ProgramData\MySQL\MySQL Server 8.0\Uploads −
java.txt:
Java is a high-level programming language originally developed by Sun Microsystems and released in 1995. Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.
Javafx.txt:
JavaFX is a Java library used to build Rich Internet Applications. The applications written using this library can run consistently across multiple platforms. The applications developed using JavaFX can run on various devices such as Desktop Computers, Mobile Phones, TVs, Tablets, etc.
coffee.txt:
CoffeeScript is a light weight language which transcompiles into JavaScript. It provides better syntax avoiding the quirky parts of JavaScript, still retaining the flexibility and beauty of the language.
opencv.txt:
OpenCV is a cross-platform library using which we can develop real-time computer vision applications. It mainly focuses on image processing, video capture and analysis including features like face detection and object detection.
现在,让我们将上述文本文件的内容加载为Tutorials_table中"Contents"列的值 -
UPDATE Tutorials_table SET Contents = LOAD_FILE('C:/ProgramData/mysql8/MySQL Server 8.0/Uploads/java.txt') WHERE ID=1; UPDATE Tutorials_table SET Contents = LOAD_FILE('C:/ProgramData/mysql8/MySQL Server 8.0/Uploads/javafx.txt') WHERE ID=2; UPDATE Tutorials_table SET Contents = LOAD_FILE('C:/ProgramData/mysql8/MySQL Server 8.0/Uploads/coffee.txt') WHERE ID=3; UPDATE Tutorials_table SET Contents = LOAD_FILE('C:/ProgramData/mysql8/MySQL Server 8.0/Uploads/opencv.txt') WHERE ID=4;
您可以使用以下 SELECT 查询验证表的内容 -
SELECT * FROM Tutorials_table;
您可以观察到插入的数据,如下表所示 -
ID | Title | Contents |
---|---|---|
1 | Java | Java is a high-level programming language originally developed by Sun Microsystems and released in 1995. Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX. |
2 | JavaFX | JavaFX is a Java library used to build Rich Internet Applications. The applications written using this library can run consistently across multiple platforms. The applications developed using JavaFX can run on various devices such as Desktop Computers, Mobile Phones, TVs, Tablets, etc. |
3 | Coffee Script | CoffeeScript is a light weight language which transcompiles into JavaScript. It provides better syntax avoiding the quirky parts of JavaScript, still retaining the flexibility and beauty of the language. |
4 | OpenCV | OpenCV is a cross-platform library using which we can develop real-time computer vision applications. It mainly focuses on image processing, video capture and analysis including features like face detection and object detection. |
要对"内容"列执行全文搜索,请向需要搜索的表列添加全文索引 -
ALTER TABLE Tutorials_table ADD FULLTEXT(Contents);
以下是获得的输出 -
Query OK, 0 rows affected, 1 warning (0.32 sec) Records: 0 Duplicates: 0 Warnings: 1
现在,您可以使用 MATCH 运算符在"Contents"列中搜索特定字符串,例如"Java",并在找到匹配项时检索完整记录 -
SELECT * FROM Tutorials_table WHERE MATCH (Contents) AGAINST ('Java');
生成的结果如下所示 -
ID | Title | Contents |
---|---|---|
1 | Java | Java is a high-level programming language originally developed by Sun Microsystems and released in 1995. Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX. |
2 | JavaFX | JavaFX is a Java library used to build Rich Internet Applications. The applications written using this library can run consistently across multiple platforms. The applications developed using JavaFX can run on various devices such as Desktop Computers, Mobile Phones, TVs, Tablets, etc. |
使用通配符的匹配运算符
MySQL 匹配运算符还可以与通配符(例如 % 和 _)一起使用,以便在全文搜索中搜索特定模式。
示例
假设之前创建的表为"Tutorials_table",我们使用以下查询来搜索以"JavaFX"开头的字符串 -
SELECT * FROM Tutorials_table WHERE MATCH (Contents) AGAINST ('%JavaFX');
上述代码的输出如下 -
ID | Title | Contents |
---|---|---|
2 | JavaFX | JavaFX is a Java library used to build Rich Internet Applications. The applications written using this library can run consistently across multiple platforms. The applications developed using JavaFX can run on various devices such as Desktop Computers, Mobile Phones, TVs, Tablets, etc. |
使用 REGEXP 的匹配运算符
您也可以将 MATCH 运算符与正则表达式 (REGEXP) 结合使用。这里,我们在 Tutorials_table 中搜索 Contents 列中包含"HTML"或"JavaFX"的字符串 -
SELECT * FROM Tutorials_table WHERE MATCH (Contents) AGAINST ('HTML|JavaFX');
执行上述代码后,我们得到以下输出 -
ID | Title | Contents |
---|---|---|
2 | JavaFX | JavaFX is a Java library used to build Rich Internet Applications. The applications written using this library can run consistently across multiple platforms. The applications developed using JavaFX can run on various devices such as Desktop Computers, Mobile Phones, TVs, Tablets, etc. |