字符串模式匹配 MySQL
String Pattern Matching MySQL
我试图找出在这种情况下查询 MySQL 数据库的最佳方法。假设我有一个包含用户个人信息的 Table。我要查询的字段是Description
栏目内容:
[1] John Loves to play music
[2] John hates music
[3] David enjoys listening to music
[4] Michael enjoys dancing.
我在表单中有一个输入字段,用户将在其中输入 "John music",它应该 return 前两个结果。
我目前正在使用这个查询:
SELECT * from TABLE WHERE description LIKE '%$input_field%'
这显然对我不起作用,因为它只会 return 字符串中包含 "John music" 的结果。
我怎样才能达到我想要的?
更新:
第二种情况是如何获取所有包含 John 或 Music 的结果。那将 return 前三个结果。
我该怎么做?
为了获得最大的灵活性,您应该查看 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%'
您可以使用 MySQL full Text search:
在这里你不需要将较大的输入字符串分解成一个数组来与每个单独的单词进行比较。
请参考下面的例子自己写:
我还想向您解释一下 Boolean Full Text Search; But I advise you to please go through Full Text Search using Query Expansion。
我们来看例子table:
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" 或短语 "database comparison" 的行,我们应该使用此请求:
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 ... |
+----+---------------------+------------------------------------------+
确保您要搜索的字词不在 list of stopwords 中,这些字词将被忽略。
我试图找出在这种情况下查询 MySQL 数据库的最佳方法。假设我有一个包含用户个人信息的 Table。我要查询的字段是Description
栏目内容:
[1] John Loves to play music
[2] John hates music
[3] David enjoys listening to music
[4] Michael enjoys dancing.
我在表单中有一个输入字段,用户将在其中输入 "John music",它应该 return 前两个结果。
我目前正在使用这个查询:
SELECT * from TABLE WHERE description LIKE '%$input_field%'
这显然对我不起作用,因为它只会 return 字符串中包含 "John music" 的结果。
我怎样才能达到我想要的?
更新:
第二种情况是如何获取所有包含 John 或 Music 的结果。那将 return 前三个结果。
我该怎么做?
为了获得最大的灵活性,您应该查看 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%'
您可以使用 MySQL full Text search:
在这里你不需要将较大的输入字符串分解成一个数组来与每个单独的单词进行比较。
请参考下面的例子自己写:
我还想向您解释一下 Boolean Full Text Search; But I advise you to please go through Full Text Search using Query Expansion。
我们来看例子table:
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" 或短语 "database comparison" 的行,我们应该使用此请求:
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 ... |
+----+---------------------+------------------------------------------+
确保您要搜索的字词不在 list of stopwords 中,这些字词将被忽略。