如何 select 包含用户输入的列?
How to select a column which contains user input?
我想对数据库中标题以某种方式包含用户输入的 select 个元素执行 MYSQL 查询。为了了解我想要什么:
数据库中的标题可能是
"The three kids are playing in the yard".
用户可能会搜索 "kids playing",我希望他能找到该标题。目前我正在使用
SELECT title FROM someTable WHERE title LIKE "%sometitle%"`
然而,这不会 return 标题,因为用户输入中缺少 "are" 一词,因此不会 return 编辑任何内容。
当用户没有输入与标题中完全相同的词序时,有没有办法 select 标题?
一种简单的方法是将搜索词拆分为 'kids' 和 'playing',然后动态生成 LIKE 语句,如下所示:
SELECT title FROM someTable WHERE title LIKE '%FIRST%SECOND%'
这里FIRST会被孩子们代替,SECOND会被玩耍代替。所以实际上你会在每个搜索词之间插入一个 %。
或者,您也可以使用 MySQL full Text search:
或者,Boolean Full Text Search Or Full Text Search using Query Expansion。
我们来看看Boolean Full Text Search;
table 示例 dev.mysql.com:
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 中,它们将被忽略。
(显然像 'is'、'the' 这样的词是 stopwords 并且这些被忽略了)
要在布尔模式下增强结果排序,您可以使用以下查询:
(假设你在用户输入的字符串中总共有 2 个单词)那么。
SELECT column_names, MATCH (text) AGAINST ('word1 word2')
AS col1 FROM table1
WHERE MATCH (text) AGAINST ('+word1 +word2' in boolean mode)
order by col1 desc;
(如果你在用户输入的字符串中有3个词)那么..
SELECT column_names, MATCH (text) AGAINST ('word1 word2 word3')
AS col1 FROM table1
WHERE MATCH (text) AGAINST ('+word1 +word2 +word3' in boolean mode)
order by col1 desc;
首先使用MATCH()
我们得到非布尔搜索模式的分数(更有特色)。 second MATCH()
确保我们真的只得到我们想要的结果(所有 3 个词)。
我想对数据库中标题以某种方式包含用户输入的 select 个元素执行 MYSQL 查询。为了了解我想要什么:
数据库中的标题可能是
"The three kids are playing in the yard".
用户可能会搜索 "kids playing",我希望他能找到该标题。目前我正在使用
SELECT title FROM someTable WHERE title LIKE "%sometitle%"`
然而,这不会 return 标题,因为用户输入中缺少 "are" 一词,因此不会 return 编辑任何内容。 当用户没有输入与标题中完全相同的词序时,有没有办法 select 标题?
一种简单的方法是将搜索词拆分为 'kids' 和 'playing',然后动态生成 LIKE 语句,如下所示:
SELECT title FROM someTable WHERE title LIKE '%FIRST%SECOND%'
这里FIRST会被孩子们代替,SECOND会被玩耍代替。所以实际上你会在每个搜索词之间插入一个 %。
或者,您也可以使用 MySQL full Text search:
或者,Boolean Full Text Search Or Full Text Search using Query Expansion。
我们来看看Boolean Full Text Search;
table 示例 dev.mysql.com:
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 中,它们将被忽略。
(显然像 'is'、'the' 这样的词是 stopwords 并且这些被忽略了)
要在布尔模式下增强结果排序,您可以使用以下查询:
(假设你在用户输入的字符串中总共有 2 个单词)那么。
SELECT column_names, MATCH (text) AGAINST ('word1 word2')
AS col1 FROM table1
WHERE MATCH (text) AGAINST ('+word1 +word2' in boolean mode)
order by col1 desc;
(如果你在用户输入的字符串中有3个词)那么..
SELECT column_names, MATCH (text) AGAINST ('word1 word2 word3')
AS col1 FROM table1
WHERE MATCH (text) AGAINST ('+word1 +word2 +word3' in boolean mode)
order by col1 desc;
首先使用MATCH()
我们得到非布尔搜索模式的分数(更有特色)。 second MATCH()
确保我们真的只得到我们想要的结果(所有 3 个词)。