【问题标题】:MySQL query : search keyword like % functionMySQL 查询:搜索关键字,如 % 函数
【发布时间】:2015-05-06 07:26:32
【问题描述】:

我的数据库中有 foo 关键字。但现在我想这样搜索

 Select id from table where name like '%foo function%'

这样的数据表

-----------------
|id  |name   |
-----------------
|1   |foo    |
|2   |sourc  |
--------------

现在我想要“foo”字符串与数据库“foo”结果匹配的“foo function”字符串请告诉我该怎么做。

【问题讨论】:

  • 实际上很难理解你在问什么。您能否添加一些示例表数据和预期结果。
  • 请检查我添加它。

标签: mysql string keyword


【解决方案1】:

大胆猜测,但也许你想要相反的 LIKE:

Select id from table where 'foo function' like '%' || name || '%'

|| 是 ANSI SQL 连接。一些 dbms 使用 concat('%', name, '%')'%' + name + '%'

【讨论】:

    【解决方案2】:

    我能理解的是,即使你输入“foo function”,你也应该得到 foo 结果。 如果那是您正在寻找的内容,那么您将不得不像(在 mysql 中)一样编写它

    name1=foo
    name2=function
    
    Select id from table where name like (concat('%',name1,'%') or      
    concat('%',name2,'%'));
    

    否则您将不得不实现像 solr 这样的搜索引擎,它为您提供模糊搜索和其他功能

    【讨论】:

      【解决方案3】:

      试试这个

      功能

         CREATE  FUNCTION [dbo].[fn_Split](@text varchar(8000), @delimiter varchar(20))
          RETURNS @Strings TABLE
          (   
            position int IDENTITY PRIMARY KEY,
            value varchar(8000)  
          )
          AS
          BEGIN
      
          DECLARE @index int
          SET @index = -1
      
          WHILE (LEN(@text) > 0)
            BEGIN 
              SET @index = CHARINDEX(@delimiter , @text) 
              IF (@index = 0) AND (LEN(@text) > 0) 
                BEGIN  
                  INSERT INTO @Strings VALUES (@text)
                    BREAK 
                END 
              IF (@index > 1) 
                BEGIN  
                  INSERT INTO @Strings VALUES (LEFT(@text, @index - 1))  
                  SET @text = RIGHT(@text, (LEN(@text) - @index)) 
                END 
              ELSE
                SET @text = RIGHT(@text, (LEN(@text) - @index))
              END
            RETURN
          END
      

      查询

       Select id from table inner join (select value from fn_split('foo function',' '))
      as split_table on table.name like '%'+split_table.value+'%';
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-11-21
        • 1970-01-01
        • 1970-01-01
        • 2010-10-07
        • 1970-01-01
        • 1970-01-01
        • 2014-11-02
        相关资源
        最近更新 更多