问题描述
我有一个使用 MATCH AGAINST
的存储过程,它最初返回按相关性排序的数据,这正是我想要的。但是,当我添加一个 LEFT JOIN
和第二个 MATCH AGAINST
时,它现在从两者都返回数据,但不再按相关性排序。现在会自动按 ID 排序。
从一个按相关性排序的表格返回:
BEGIN
SELECT
dictionary.id,dictionary.`term`,dictionary.`deFinition`
FROM itDictionary dictionary
WHERE MATCH(dictionary.term,dictionary.deFinition)
AGAINST(_query IN NATURAL LANGUAGE MODE);
END
从两个表返回但以某种方式按 ID 排序:
BEGIN
SELECT
dictionary.id,dictionary.`deFinition`,links.id,links.`name`,links.`url`
FROM itDictionary dictionary
LEFT JOIN itRelatedLinks links ON links.term_id = dictionary.id
WHERE MATCH(dictionary.term,dictionary.deFinition)
AGAINST(_query IN NATURAL LANGUAGE MODE)
OR MATCH(links.name)
AGAINST(_query IN NATURAL LANGUAGE MODE);
END
我很亲近吗?
解决方法
你可以这样做
SELECT
dictionary.id,dictionary.`term`,dictionary.`definition`,links.id,links.`name`,links.`url`
FROM itDictionary dictionary
LEFT JOIN itRelatedLinks links ON links.term_id = dictionary.id
WHERE MATCH(dictionary.term,dictionary.definition)
AGAINST(_query IN NATURAL LANGUAGE MODE)
OR MATCH(links.name)
AGAINST(_query IN NATURAL LANGUAGE MODE)
ORDER BY MATCH(dictionary.term,dictionary.definition)
AGAINST(_query IN NATURAL LANGUAGE MODE)
+ MATCH(links.name)
AGAINST(_query IN NATURAL LANGUAGE MODE);
它会将两者的分数相加,并得到一个组合分数来排序
当然,您可以为每个分数添加一个因素来确定优先级