3

不确定这是否可能

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

有没有办法做到这一点。

提前感谢您的帮助

4

2 回答 2

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 语句。如果您想同时显示两列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);
于 2013-10-08T07:49:14.333 回答
0

有一个案例/如果。

concat(
   if(yourcolumn like"%keyword%", 'keyword ', ''),
   if(yourcolumn like"%keyword2%", 'keyword2 ', ''),
   if(yourcolumn like"%keyword3%", 'keyword3 ', '')
) as found_keywords
于 2013-10-08T07:36:39.563 回答