必须匹配MATCH AGAINST mysql中的所有单词

时间:2017-11-20 10:14:06

标签: mysql match-against

SELECT *,
MATCH (`text`) AGAINST ('my work involves for' IN BOOLEAN MODE) `score`
FROM `messages` WHERE MATCH (`must_match`) AGAINST ('my work involves for' IN BOOLEAN MODE)
ORDER BY `score` DESC

所以这里的用户输入是'我的工作涉及'

id   text                 must_match
---  ----                 ------
1    my work is to help   work,help
2    work involves help   involves,work

现在我想要结果如果mus_match列的所有单词在用户输入中匹配 所以,从上面一行我们的结果将是第二行

must_match列将包含文本列

中的单词

1 个答案:

答案 0 :(得分:0)

花了很多时间后,我无法为您的任务提出解决方案/解决方法。这背后的主要原因是您的表中有多值属性must_match。它包含以逗号分隔的值,并且还未预定义值的数量。

首先,我将要求您通过this link以实现在列中存储分隔列表的缺点。

id        text1         must_match
1   my work is to help  work
1   my work is to help  help
2   work involves help  involves
2   work involves help  work

现在,如果您能够在1NF中转换表并在多行中存储多个must_match值(如上所述),那么请继续阅读解决方案,如果没有,请注释背后的原因。< / p>

<强>解决方案:

select id,text1,
MATCH (text1) AGAINST ('my work involves for' IN BOOLEAN MODE) as score
from t t1
where locate(must_match,"my work involves for") > 0
group by id
having count(*) = (select count(*) from t where id = t1.id);

注意:如果id主键,那么您也可以group by使用text列。

Click here for Demo

希望它有所帮助!