2013-10-08 69 views
3

不知道這是可能的Mysql的對陣查詢 - 顯示在結果匹配關鍵字

我有一大堆的100個關鍵字中,我運行一個對陣查詢,看看這些關鍵字存在於表 - 查詢工作正常:

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。

有沒有辦法做到這一點。

預先感謝 感謝您的幫助

+0

這是在PHP中嗎? –

+0

我直接在數據庫上運行這個查詢來獲取報告。 – Gublooo

回答

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