在mysql全文搜索中,匹配项给出了一些值,我不知道该值提到了哪一个?
我需要以百分比形式显示比较输出,如何实现?

选择ID,标题,正文,MATCH(标题,正文)
     反对(“数据库”)来自“匹配”文章(标题,正文)
     反对(“数据库”);

输出:

id',   'title',                    'body',                     'MATCH'
1, 'MYSql Tutorial', 'My Sql is one of the database language', 0.93769526481628
10, 'MySQL vs. YourSQL', 'In the following database comparison ...', 0.93769526481628
6, 'MySQL Tutorial', 'DBMS stands for DataBase ...', 0.92749810218811

最佳答案

我认为您的意思是您想要相关性以及结果。看起来像:

SELECT id, title, body, MATCH(title, body) AGAINST ('database') as Relevance FROM `articles` WHERE MATCH(title, body) AGAINST ('database' IN BOOLEAN MODE) ORDER BY `Relevance` DESC


编辑我弄错了问题。以下是所有相关性的百分比形式的列表:

SELECT a.id, a.title, a.body,
MATCH(a.title) AGAINST ('database') as titleRelevance,
MATCH(a.body) AGAINST ('database') as bodyRelevance,
MATCH(a.title) AGAINST ('database')/c.maxTitleRelevance *100 AS percentageTitleRelevance,
MATCH(a.body) AGAINST ('database')/d.maxBodyRelevance *100 AS percentageBodyRelevance,
c.maxTitleRelevance + d.maxBodyRelevance AS maxTotalRelevance,
(MATCH(a.title) AGAINST ('database')+MATCH(a.body) AGAINST ('database'))/(c.maxTitleRelevance + d.maxBodyRelevance)*100 AS percentageTotalRelevance
FROM `articles` a,
(SELECT MAX(MATCH(b.title) AGAINST('database')) as maxTitleRelevance FROM articles b LIMIT 1) c,
(SELECT MAX(MATCH(b.body) AGAINST('database')) as maxBodyRelevance FROM articles b LIMIT 1) d
WHERE MATCH(a.title, a.body) AGAINST ('database' IN BOOLEAN MODE)


在下面的小提琴中可能更容易阅读。 http://sqlfiddle.com/#!2/c7885/14
mysql中的全文搜索充其量是脆弱的。如果单词数据库至少在数据库行的50%中,如果不像上面的查询中那样指定布尔模式,它将被忽略为常见的停用词。关于相关性的计算方式,我恐怕无法为您提供帮助。

10-06 03:20