我以前使用过Sql Server并使用了它非常好的功能,称为Sql全文搜索。现在我必须使用MySql。谁能告诉我MySql中的全文搜索等价物?我使用的是免费版的MySql而不是商业版。如果没有,那么我们还能做些什么来模仿全文搜索并克服LIKE算子的局限?
提前致谢:)
答案 0 :(得分:3)
请参阅mysql中有关全文搜索支持的文档:
http://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html
您需要将全文索引应用于列,以支持对这些列进行全文搜索。请注意,mysql仅支持MyISAM表的全文索引。
作为一个例子,以防上述链接在某些时候失效,请参阅文档中的完整示例:
mysql> CREATE TABLE articles (
-> id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
-> title VARCHAR(200),
-> body TEXT,
-> FULLTEXT (title,body)
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO articles (title,body) VALUES
-> ('MySQL Tutorial','DBMS stands for DataBase ...'),
-> ('How To Use MySQL Well','After you went through a ...'),
-> ('Optimizing MySQL','In this tutorial we will show ...'),
-> ('1001 MySQL Tricks','1. Never run mysqld as root. 2. ...'),
-> ('MySQL vs. YourSQL','In the following database comparison ...'),
-> ('MySQL Security','When configured properly, MySQL ...');
Query OK, 6 rows affected (0.00 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM articles
-> WHERE MATCH (title,body)
-> AGAINST ('database' IN NATURAL LANGUAGE MODE);
+----+-------------------+------------------------------------------+
| id | title | body |
+----+-------------------+------------------------------------------+
| 5 | MySQL vs. YourSQL | In the following database comparison ... |
| 1 | MySQL Tutorial | DBMS stands for DataBase ... |
+----+-------------------+------------------------------------------+
参考http://dev.mysql.com/doc/refman/5.1/en/fulltext-natural-language.html
答案 1 :(得分:2)
在MyISAM
表上,MySQL
支持FULLTEXT
索引:
CREATE TABLE mytable (id INT, caption TEXT, content TEXT, FULLTEXT KEY fx_mytable_caption_content (caption, content)) ENGINE=MyISAM;
SELECT *
FROM mytable
WHERE MATCH(caption, content) AGAINST ('my search query')
答案 2 :(得分:1)
您可以在一列或多列上创建全文索引。然后,您可以使用全文搜索查询来查询表。请参阅MySQL Full-Text index和MySQL Full-text Search functions。
典型的查询如下:
SELECT * FROM books
WHERE MATCH (title, summary) AGAINST ('harry potter')
ORDER BY MATCH (title, summary) AGAINST ('harry potter')
LIMIT 0, 10