2016-04-30 23 views
-2

[tbl_post]id,說明[long_text]子串IN mysql需要取詞

描述字段具有以下值:

Hi how r u? #TravelingtheWorld #TravelingtheWorld 
Hi how r u? #Traveled 
bla bla bla travelbybus blah blah 
blah blah blah @travelegency blah blah 
the intelligence demonstrated in during tests iterations 
blah blah blah testing blahh 
blah blah blah tested blahh 

我們有@功能。

如果我嘗試搜索的價值 「測試」,我需要 「測試/檢驗/檢測」

SELECT SUBSTRING_INDEX(CONCAT(
      '$ipstring', 
      SUBSTRING_INDEX(LOWER('description'), LOWER('$ipstring'), -1) 
     ) , ' ',1) AS 'mystring' 
FROM tbl_post 
WHERE `description` LIKE '$ipstring%' 
    OR `description` LIKE '% $ipstring%' 
    OR `description` LIKE '%#$ipstring%' 
    OR `description` LIKE '%@$ipstring%' 
  1. 輸入搜索字符串的響應

    $ipstring = "travel" 
    

    我在這裏獲得輸出

    • 已旅行
    • TravelingtheWorld
    • travelbybus
    • travelegency
  2. 輸入搜索字符串

    $ipstring = "te" 
    

    我在這裏獲得輸出

    • terations >>>在這裏,我需要 「測試」(即智能在測試迭代期間顯示)
    • 測試
    • 測試

查詢:

  1. 首先查詢

    SELECT SUBSTRING_INDEX (CONCAT(
          'travel', 
          SUBSTRING_INDEX(LOWER('Hi how r u? *TravelingtheWorld *TravelingtheWorld'), LOWER('travel'), -1) 
         ) , ' ',1) AS 'mystring' 
    
  2. 第二個查詢

    SELECT SUBSTRING_INDEX (CONCAT(
          'te', 
          SUBSTRING_INDEX(LOWER('the intelligence demonstrated in during tests iterations'), LOWER('te'), -1) 
         ) , ' ',1) AS 'mystring' 
    

如果我添加一個空格「」之前,輸入字符串「TE」,我會得到「測試」

SELECT SUBSTRING_INDEX (CONCAT(
       'te', 
       SUBSTRING_INDEX(LOWER('the intelligence demonstrated in during tests iterations'), LOWER(' te'), -1) 
      ) , ' ',1) AS 'mystring' 

但對於第一次查詢:如果我添加一個空格「輸入字符串之前」 'travelling'

SELECT SUBSTRING_INDEX (CONCAT(
       'travel', 
       SUBSTRING_INDEX(LOWER('Hi how r u? *TravelingtheWorld *TravelingtheWorld'), LOWER(' travel'), -1) 
      ) , ' ',1) AS 'mystring' 

...我沒有收到「TravelingtheWorld」

+0

我看到'$ ipstring':你使用PHP嗎?如果是這樣,PHP解決方案適合你嗎? – trincot

+0

是的,我們正在使用php Mysql但我們需要在單個查詢中輸出mysql而不是在php中。在此先感謝 –

+0

即使不是,如果您將PHP函數作爲參數查詢結果集並將修改它並將其重新返回?在純粹的MySql中,這將是巨大的代碼,我擔心效率會更低。 – trincot

回答

0

我建議這個查詢:

select * 
from (
     select description, 
       substring_index(
       substr(
        description, 
        instr(
        concat(
         ' ', 
         replace(
         replace(lower(description), '@', ' '), 
         '#', 
         ' ' 
         ), 
         ' ' 
        ), 
        ' travel' 
        ) 
       ), 
       ' ', 
       1 
       ) as extract 
     from tbl_post 
     ) as m 
where length(m.extract) > 0 

你將不得不提供像' te'' travel'前綴區域的搜索詞。

+0

@ pradeepandrewson2,這是否解決了您的問題?你可以提供一些反饋嗎? – trincot

+0

是的,它解決了謝謝你 –