Mysql Match Against query - 在结果中显示匹配的关键字

时间:2013-10-08 07:26:34

标签: mysql sql match-against

不确定这是否可行

我有一堆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。

无论如何都要这样做。

提前致谢 感谢您的帮助

2 个答案:

答案 0 :(得分:1)

试试这个,它对我来说很好。

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);

您可以分别对column2使用第二个case语句。 如果要同时显示matches_value列,请使用this-

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);

答案 1 :(得分:0)

带案例/如果。

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