MYSQL选择字符串中所有子字符串的位置

时间:2015-05-30 10:17:21

标签: php mysql sql

我将字符串存储在表格中:

1. "the quick brown fox"
2. "the quick brown fox jumps"
3. "the quick brown fox jumps over the lazy dog"
4. "the quick potato does nothing"

给定三个输入词我想在字符串

中找到所有三个单词时返回该条目

所以我这样做:

WHERE word1 IN stringfield AND word2 IN stringfield AND word3 IN stringfeild

但是,我想选择性地提供额外的输入字,以便通过包含与输入字最匹配的条目来过滤结果。所以所有返回的比赛将至少有三场比赛。

例如输入单词:

"the", "quick", "brown", "fox", "jumps", "over"

返回:

3.
2. 
1. 

因为3匹配最多,然后是2,然后是1.而且4没有被选中,因为它不包含至少三个匹配。

那可能吗?这是最快的方法,还是我会更好地使用联结表?如果是这样的话?非常感谢。

2 个答案:

答案 0 :(得分:1)

首先,您可能最好使用MySQL全文功能。阅读它here

我假设您正在动态构建where子句,因此如果您有五个单词,则可以构造:

WHERE stringfield LIKE '%word1%' OR
      stringfield LIKE '%word2%' OR
      stringfield LIKE '%word3%' OR
      stringfield LIKE '%word4%' OR
      stringfield LIKE '%word5%' 

IN运算符根本无法执行您认为的操作。

如果你可以这样做,那么完整的查询也会有:

WHERE ((stringfield LIKE '%word1%') +
       (stringfield LIKE '%word2%') +
       (stringfield LIKE '%word3%') +
       (stringfield LIKE '%word4%') +
       (stringfield LIKE '%word5%')
      ) >= 3
ORDER BY ((stringfield LIKE '%word1%') +
          (stringfield LIKE '%word2%') +
          (stringfield LIKE '%word3%') +
          (stringfield LIKE '%word4%') +
          (stringfield LIKE '%word5%')
         ) DESC

MySQL将布尔表达式视为数字上下文中的整数。这使得计算匹配数量变得特别容易。但是,正如我所说,全文索引可能是你真正需要的。

答案 1 :(得分:1)

当我开始认为某些事情很艰难时,我会尝试完成它...... 这是一个解决方案:(数据库的名称是'测试')

1创建此功能

DELIMITER $$
CREATE DEFINER = 'root'@'%'
FUNCTION Test.countOccurence (LineTocheck nvarchar(255), criteriaToMatch nvarchar(15))
RETURNS int(11)
BEGIN
  DECLARE Occurences int DEFAULT 0;
  SELECT
    (LENGTH(LineTocheck) - LENGTH(REPLACE(LineTocheck, criteriaToMatch, ''))) / LENGTH(criteriaToMatch) INTO Occurences;
  RETURN Occurences;
END
$$

DELIMITER ;

第二次执行查询:

SELECT Generic.id
    ,Description
    ,SUM(countOccurence(Description, c.criteria))
FROM Generic
    ,criteria c
GROUP BY Description
    ,Generic.id
ORDER BY SUM(countOccurence(Description, c.criteria)) desc

P.S。表结构是: 对于标准:

CREATE TABLE Test.criteria (
  id int(11) NOT NULL AUTO_INCREMENT,
  criteria varchar(15) NOT NULL,
  PRIMARY KEY (id)
)
ENGINE = INNODB
AUTO_INCREMENT = 1
CHARACTER SET utf8
COLLATE utf8_general_ci;

对于您要搜索Occurences的表格

CREATE TABLE Test.Generic (
  id int(11) NOT NULL AUTO_INCREMENT,
  Description varchar(255) NOT NULL,
        PRIMARY KEY (id)
)
ENGINE = INNODB
AUTO_INCREMENT = 1
CHARACTER SET utf8
COLLATE utf8_general_ci;

SET NAMES 'utf8';

INSERT INTO Test.criteria(id, criteria) VALUES
(1, 'fox');
INSERT INTO Test.criteria(id, criteria) VALUES
(2, 'brown');
INSERT INTO Test.criteria(id, criteria) VALUES
(3, 'over');

SET NAMES 'utf8';

INSERT INTO Test.Generic(id, Description) VALUES
(1, 'the quick brown fox');
INSERT INTO Test.Generic(id, Description) VALUES
(2, 'the quick brown fox jumps');
INSERT INTO Test.Generic(id, Description) VALUES
(3, 'the quick brown fox jumps over the lazy dog');
INSERT INTO Test.Generic(id, Description) VALUES
(4, 'the quick potato does nothing');

使用Dbforge MySQL Studio Express(免费)连接MySQL并运行语句 http://www.devart.com/login.html?returnToUrl=/dbforge/mysql/studio/download.html%3Ffd=dbforgemysqlfree.exe

测试并让我知道