Mysql 匹配查询 - 在结果中显示匹配的关键字



不确定这是否可能

有一堆 100 个关键字,我正在运行 MATCH AGAINST 查询以查看表中是否存在这些关键字 - 查询工作正常:

SELECT * FROM questions_new WHERE MATCH (question_title,question)
AGAINST ('depreciation amortization npv dcf "discounted cash flow" "cash flow statement" "current assets"' IN BOOLEAN MODE);

这只是一个包含很少关键字的示例查询

结果返回如下:

question_id | question_title            |  question
    1       | what is depreciation      |  I am trying to do this DCF calculation......
    2       | Need help with this       |  what is a cash flow statement
    3       | Cannot solve this problem |  Can you give more examples on npv

这是一个示例结果集。显然,我的结果集要大得多。

现在查看question_title或问题 - 我很难找出匹配的关键字,因为关键字列表很大。

想知道我是否可以在结果集中包含匹配的关键字,如下所示

question_id |        keyword      | question_title            |  question
    1       | depreciation, DCF   | what is depreciation      |  I am trying to do this DCF calculation......
    2       | cash flow statement | Need help with this       |  what is a cash flow statement
    3       | npv                 | Cannot solve this problem |  Can you give more examples on npv

如您所见,第一个结果显示匹配的 2 个关键字 - 折旧和 DCF。

无论如何可以做到这一点。

提前致谢感谢您的帮助

试试这个,它对我来说工作正常。

SELECT column1, column2,
cASE when column1 like '%word1%' then "word1" 
when column1 like '%word2%' then "word2" 
when column1 like '%word3%' then "word3" 
when column1 like '%word4%' then "word4" end  as matched_word
FROM table_name WHERE MATCH (column1,column2)
AGAINST ('"word1" "word2" "word3" "word4"' IN BOOLEAN MODE);

您可以分别对列 2 使用第二个大小写语句。如果要同时显示两列matched_value请使用此-

SELECT column1, column2,
ifnull((cASE when column1 like '%word1%' then "word1" 
when column1 like '%word2%' then "word2" 
when column1 like '%word3%' then "word3" 
when column1 like '%word4%' then "word4" end),
(cASE when column2 like '%word1%' then "word1" 
when column2 like '%word2%' then "word2" 
when column2 like '%word3%' then "word3" 
when column2 like '%word4%' then "word4" end))  as matched_word
FROM table_name WHERE MATCH (column1,column2)
AGAINST ('"word1" "word2" "word3" "word4"' IN BOOLEAN MODE);

,带有大小写/if。

concat(
   if(yourcolumn like"%keyword%", 'keyword ', ''),
   if(yourcolumn like"%keyword2%", 'keyword2 ', ''),
   if(yourcolumn like"%keyword3%", 'keyword3 ', '')
) as found_keywords

最新更新