使用包含关键字的字符串在PHP中进行MySql全文搜索

Vas*_*sko 8 php mysql full-text-search

我有一个包含这样的关键字的字符串$string = 'one, two, "phrase three words"' 我正在尝试使用以下方法进行全文搜索:

    SELECT *, MATCH (column) AGAINST ('$string') AS score, 
FROM table WHERE MATCH (column) AGAINST ('$string' IN BOOLEAN MODE) ORDER BY score DESC
Run Code Online (Sandbox Code Playgroud)

当MySql返回结果时,关键字"短语三个单词"不匹配作为短语,但是来自它的每个单词都匹配为单个.

我应该如何修改字符串或查询以接收整个短语匹配的结果?我已尝试使用stripslashes()作为字符串,但结果是一样的.

use*_*035 25

正如MySQL手册所说:

括在双引号(""")字符中的短语仅匹配字面上包含短语的行,因为它是键入的.

我们来看一下示例表:

mysql> select * from articles;
+----+-----------------------+------------------------------------------+
| id | title                 | body                                     |
+----+-----------------------+------------------------------------------+
|  1 | PostgreSQL Tutorial   | DBMS stands for DataBase ...             |
|  2 | How To Use MySQL Well | After you went through a ...             |
|  3 | Optimizing MySQL      | In this tutorial we will show ...        |
|  4 | 1001 MySQL Tricks     | 1. Never run mysqld as root. 2. ...      |
|  5 | MySQL vs. YourSQL     | In the following database comparison ... |
|  6 | MySQL Security        | When configured properly, MySQL ...      |
+----+-----------------------+------------------------------------------+

mysql> SELECT * FROM articles WHERE MATCH (title,body)
     AGAINST ('"database comparison"' IN BOOLEAN MODE);

+----+-------------------+------------------------------------------+
| id | title             | body                                     |
+----+-------------------+------------------------------------------+
|  5 | MySQL vs. YourSQL | In the following database comparison ... |
+----+-------------------+------------------------------------------+
Run Code Online (Sandbox Code Playgroud)

当引用单词时,顺序很重要:

mysql> SELECT * FROM articles WHERE MATCH (title,body)
     AGAINST ('"comparison database"' IN BOOLEAN MODE);

Empty set (0.01 sec)
Run Code Online (Sandbox Code Playgroud)

当我们删除引号时,它将搜索包含"database"或"comparison"字样的行:

mysql> SELECT * FROM articles WHERE MATCH (title,body)
     AGAINST ('database comparison' IN BOOLEAN MODE);

+----+---------------------+------------------------------------------+
| id | title               | body                                     |
+----+---------------------+------------------------------------------+
|  1 | PostgreSQL Tutorial | DBMS stands for DataBase ...             |
|  5 | MySQL vs. YourSQL   | In the following database comparison ... |
+----+---------------------+------------------------------------------+
Run Code Online (Sandbox Code Playgroud)

订单现在无关紧要:

mysql> SELECT * FROM articles WHERE MATCH (title,body)
     AGAINST ('comparison database' IN BOOLEAN MODE);

+----+---------------------+------------------------------------------+
| id | title               | body                                     |
+----+---------------------+------------------------------------------+
|  1 | PostgreSQL Tutorial | DBMS stands for DataBase ...             |
|  5 | MySQL vs. YourSQL   | In the following database comparison ... |
+----+---------------------+------------------------------------------+
Run Code Online (Sandbox Code Playgroud)

如果我们想要获取包含单词"PostgreSQL"或短语"数据库比较"的行,我们应该使用此请求:

mysql> SELECT * FROM articles WHERE MATCH (title,body)
     AGAINST ('PostgreSQL "database comparison"' IN BOOLEAN MODE);

+----+---------------------+------------------------------------------+
| id | title               | body                                     |
+----+---------------------+------------------------------------------+
|  1 | PostgreSQL Tutorial | DBMS stands for DataBase ...             |
|  5 | MySQL vs. YourSQL   | In the following database comparison ... |
+----+---------------------+------------------------------------------+
Run Code Online (Sandbox Code Playgroud)

小提琴

确保您搜索的单词不在停用词列表中,但会被忽略.