参数相反的MySQL MATCH()AGAINST() [英] MySQL MATCH() AGAINST() with reversed parameters

查看:92
本文介绍了参数相反的MySQL MATCH()AGAINST()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的数据库表:

I have a database table that looks a bit like this:

id|words|url  
1|+Word +Matching -Goodbye|/url-1  
2|+Redirect +Me|/url-2  
3|+Goodbye +Word|/url-3  

当用户键入搜索内容:你好我正在匹配一个单词"时,我希望对table.words字段赋予与该字符串相关性"的分数,就像MATCH()AGAINST()一样功能,但参数相反.

When a user types a search for: "Hello I am matching a word", I would like the table.words field to be given a 'relevance' score against that string, a lot like the MATCH() AGAINST() function, but with the parameters reversed.

有效地,我要运行的查询大致如下:

Effectively, the query I am looking to run would be along the lines of:

SELECT id, words, url,
MATCH ("Hello I am matching a word") AGAINST (words IN BOOLEAN MODE) AS relevance  
FROM table
ORDER BY relevance DESC

但是,不幸的是,这行不通.我可以在PHP中做一些事情,在其中创建一个函数来遍历每个包含/不包含的单词,但是我担心,随着表大小的增加,这确实会很慢.

But this does not work, unfortunately. I could do something in PHP where I create a function to loop through each inclusive/exclusive word, but I fear that this will be really slow when the table size grows.

为了解决这个问题,我希望查询返回id:1,因为它包含"Word"和"Matching",但不包含"Goodbye".我应该指出,这些单词可以在字符串中以任意顺序排列,所以我认为我不能真正使用LIKE.

Just to tie it up, I would expect the query to return id: 1 in that instance, as it includes "Word" and 'Matching", and does not include "Goodbye". I should point out that these words could be in any order within the string, so I couldn't really use LIKE, I don't think.

如果不存在这样的功能,有没有更好的方法呢?

If such a function does not exist, is there a better way I could approach this?

谢谢!

推荐答案

搜索字符串必须是在查询过程中恒定的字符串值评估.例如,这排除了表列,因为每行可以不同.

The search string must be a string value that is constant during query evaluation. This rules out, for example, a table column because that can differ for each row.

因此,您无法执行查询所需的操作.

Hence, you cannot do what you want with a query.

可以使用动态SQL.或在PHP中循环以遍历表中的模式.

You can use dynamic SQL. Or a loop in PHP to loop through the patterns in your table.

这篇关于参数相反的MySQL MATCH()AGAINST()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆