子串IN IN mysql需要获取单词

时间:2016-04-30 11:00:49

标签: mysql sql

[tbl_post] 具有 id,description [long_text]

description 字段具有以下值:

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

我们有 @ 功能。

如果我尝试搜索值" test",我需要"测试/测试/测试"

的响应
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>>>在这里,我需要"测试"(在测试迭代期间演示的情报)
    • 测试
    • 测试
  3. 查询:

    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'
      
    3. 如果我添加空格" "在输入字符串"之前te",我将得到"测试"

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

      但是对于第一个查询:如果我添加一个空格" "在输入字符串之前'行进'

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

      ......我没有得到" TravelingtheWorld"

1 个答案:

答案 0 :(得分: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'等搜索字词。