字符串模式匹配MySQL

时间:2015-03-10 16:44:20

标签: mysql sql regex pattern-matching

我试图找出在这种情况下查询MySQL数据库的最佳方法。让我们想象一下,我有一个包含用户个人信息的表格。我想查询的字段是描述

列内容:

[1] John Loves to play music
[2] John hates music
[3] David enjoys listening to music
[4] Michael enjoys dancing.

我在表单中有一个输入字段,用户将输入“John music”,它应返回前两个结果。

我目前正在使用此查询:

SELECT * from TABLE WHERE description LIKE '%$input_field%'

这显然对我不起作用,因为它只会在字符串中返回包含“John music”的结果。

我如何实现我的目标?

更新:

第二种情况是如何获得其中包含John或Music的所有结果。这将返回第一个三个结果。

我该怎么做?

2 个答案:

答案 0 :(得分:2)

您可以使用MySQL full Text search执行此操作:

在这里,您不需要将较大的输入字符串分解为数组以与每个单词进行比较。

请参考以下示例编写自己的示例:

我想解释一下Boolean Full Text Search;但我建议你也请通过Full Text Search using Query Expansion

让我们看一下示例表:

mysql> select * from articles;
+----+-----------------------+------------------------------------------+
| id | title                 | body                                     |
+----+-----------------------+------------------------------------------+
|  1 | PostgreSQL Tutorial   | DBMS stands for DataBase ...             |
|  2 | How To Use MySQL Well | After you went through a ...             |
|  3 | Optimizing MySQL      | In this tutorial we will show ...        |
|  4 | 1001 MySQL Tricks     | 1. Never run mysqld as root. 2. ...      |
|  5 | MySQL vs. YourSQL     | In the following database comparison ... |
|  6 | MySQL Security        | When configured properly, MySQL ...      |
+----+-----------------------+------------------------------------------+

mysql> SELECT * FROM articles WHERE MATCH (title,body)
     AGAINST ('"database comparison"' IN BOOLEAN MODE);

+----+-------------------+------------------------------------------+
| id | title             | body                                     |
+----+-------------------+------------------------------------------+
|  5 | MySQL vs. YourSQL | In the following database comparison ... |
+----+-------------------+------------------------------------------+

当引用单词时,顺序很重要:

mysql> SELECT * FROM articles WHERE MATCH (title,body)
     AGAINST ('"comparison database"' IN BOOLEAN MODE);

Empty set (0.01 sec)

当我们删除引号时,它会搜索包含“database”或“comparison”字样的行:

mysql> SELECT * FROM articles WHERE MATCH (title,body)
     AGAINST ('database comparison' IN BOOLEAN MODE);

+----+---------------------+------------------------------------------+
| id | title               | body                                     |
+----+---------------------+------------------------------------------+
|  1 | PostgreSQL Tutorial | DBMS stands for DataBase ...             |
|  5 | MySQL vs. YourSQL   | In the following database comparison ... |
+----+---------------------+------------------------------------------+

现在订单无关紧要:

mysql> SELECT * FROM articles WHERE MATCH (title,body)
     AGAINST ('comparison database' IN BOOLEAN MODE);

+----+---------------------+------------------------------------------+
| id | title               | body                                     |
+----+---------------------+------------------------------------------+
|  1 | PostgreSQL Tutorial | DBMS stands for DataBase ...             |
|  5 | MySQL vs. YourSQL   | In the following database comparison ... |
+----+---------------------+------------------------------------------+

如果我们想要获取包含单词“PostgreSQL”或短语“数据库比较”的行,我们应该使用此请求:

mysql> SELECT * FROM articles WHERE MATCH (title,body)
     AGAINST ('PostgreSQL "database comparison"' IN BOOLEAN MODE);

+----+---------------------+------------------------------------------+
| id | title               | body                                     |
+----+---------------------+------------------------------------------+
|  1 | PostgreSQL Tutorial | DBMS stands for DataBase ...             |
|  5 | MySQL vs. YourSQL   | In the following database comparison ... |
+----+---------------------+------------------------------------------+

fiddle

确保您搜索的字词不在list of stopwords中,但会被忽略。

答案 1 :(得分:0)

为了获得最大的灵活性,您应该查看MySQL Full Text Functions。但是,如果您想要基本的SQL答案......

如果你想在音乐之前找到John出现的事件......

WHERE description LIKE '%john%music%'  # (i.e. change the values of your parameter)

如果您想查找包含John和Music的事件(按任意顺序排列)......

WHERE description LIKE '%john%' AND description LIKE '%music%' 
    # (i.e. you need two input parameters)

如果您想查找包含John OR音乐的事件......

WHERE description LIKE '%john%' OR description like '%music%'