加入收藏 | 设为首页 | 会员中心 | 我要投稿 北几岛 (https://www.beijidao.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2021-07-06 05:25:24 所属栏目:大数据 来源: https://www.jb51.cc
导读:我有一个字符串,其中包含一些关键字,例如$string =’one,two,“短语三个单词”’ 我正在尝试使用以下方法进行全文搜索: SELECT *, MATCH (column) AGAINST ('$string') AS score, FROM table WHERE MATCH (column) AGAINST ('$string' IN BOOLEAN MODE) ORD

我有一个字符串,其中包含一些关键字,例如$string =’one,two,“短语三个单词”’
我正在尝试使用以下方法进行全文搜索:

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

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

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

解决方法:

正如MySQL手册所说:

A phrase that is enclosed within double quote (“””) characters matches
only rows that contain the phrase literally, as it was typed.

我们来看一下示例表:

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 ... |
+----+-------------------+------------------------------------------+

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

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

Empty set (0.01 sec)

当我们删除引号时,它将搜索包含“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 ... |
+----+---------------------+------------------------------------------+

订单现在无关紧要:

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 ... |
+----+---------------------+------------------------------------------+

如果我们想要获取包含单词“Postgresql”或短语“database comparison”的行,我们应该使用此请求:

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 ... |
+----+---------------------+------------------------------------------+

fiddle

确保您正在搜索的单词不在list of stopwords中,但会被忽略.

(编辑:北几岛)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读