mysql全文搜索只得分高于零

hgc7kmma  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(151)

如果我有这样的全文查询:

SELECT title,
   MATCH(question) AGAINST ('string') AS q_score,
   MATCH(answer) AGAINST ('string') AS a_score,
   MATCH(keywords) AGAINST ('string') AS kw_score FROM helptable
   ORDER BY (4 * q_score) + (1 * a_score) + (2 * kw_score) DESC

这样的话,我得到了一个相关性排名的结果。
不过,我不希望任何综合得分为0的结果。但是当我尝试添加一些东西时,比如:

WHERE a_score > 0

它不起作用(“where子句”中的“a#u score”未知列)。有别的方法吗?谢谢您!
(mariadb 10.4.10,innodb表)

yshpjwxd

yshpjwxd1#

你可以使用 having 条款:

SELECT title,
       MATCH(question) AGAINST ('string') AS q_score,
       MATCH(answer) AGAINST ('string') AS a_score,
       MATCH(keywords) AGAINST ('string') AS kw_score
FROM helptable
HAVING a_score > 0
ORDER BY (4 * q_score) + (1 * a_score) + (2 * kw_score) DESC;

mysql扩展了 HAVING 子句,以便可以在非聚合查询中使用。mysql还允许在 HAVING 条款。瞧!这就是你想要的。
我不知道你所说的“综合得分”是什么意思,但你也可以添加附加条件:

HAVING a_score > 0 OR kw_score > 0

相关问题