php mysql 包含字符串_使用包含关键字的字符串在PHP中进行MySQL全文搜索

bd96500e110b49cbb3cd949968f18be7.png

I have a string that contains some keywords like this $string = 'one, two, "phrase three words"'

I am trying to do a full-text search using:

SELECT *, MATCH (column) AGAINST ('$string') AS score,

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

When MySql gives back the results, the keyword "phrase three words" is not matched as a phrase, but every word from it is matched as a single.

How should I modify the string or the query to receive the results where the whole phrase matches? I have tried with stripslashes() for the string, but the result is the same.

解决方案

As MySQL manual says:

A phrase that is enclosed within double quote (“"”) characters matches

only rows that contain the phrase literally, as it was typed.

Let's look at the example table:

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

+----+-------------------+------------------------------------------+

Order matters, when the words are quoted:

mysql> SELECT * FROM articles WHERE MATCH (title,body)

AGAINST ('"comparison database"' IN BOOLEAN MODE);

Empty set (0.01 sec)

When we remove the quotes, it will search for rows, containing words "database" or "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 ... |

+----+---------------------+------------------------------------------+

Order doesn't matter now:

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

+----+---------------------+------------------------------------------+

If we want to get rows, containing either word "PostgreSQL" or phrase "database comparison", we should use this request:

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

+----+---------------------+------------------------------------------+

Make sure, that the words, you are searching for, are not in the list of stopwords, that are ignored.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值