MySQL全文查询的方法并不难,下面就为您详细介绍MySQL全文查询的实现方法,希望对您学习MySQL全文查询方面能够有所帮助。
到3.23.23时,MySQL开始支持全文索引和搜索。全文索引在MySQL中是一个FULLTEXT类型索引。FULLTEXT索引用于 MyISAM 表,可以在CREATE TABLE时或之后使用ALTER TABLE或 CREATE INDEX 在 CHAR、VARCHAR 或 TEXT 列上创建。对于大的数据库,将数据装载到一个没有FULLTEXT索引的表中,然后再使用 ALTER TABLE (或 CREATE INDEX) 创建索引,这将是非常快的。将数据装载到一个已经有FULLTEXT索引的表中,将是非常慢的。
全文搜索通过 MATCH() 函数完成。
- 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 VALUES
- -> (NULL,\'MySQLTutorial\', \'DBMS stands for DataBase ...\'),
- -> (NULL,\'How To UseMySQLEfficiently\', \'After you went through a ...\'),
- -> (NULL,\'OptimisingMySQL\',\'In this tutorial we will show ...\'),
- -> (NULL,\'1001MySQLTricks\',\'1. Never runMySQLd as root. 2. ...\'),
- -> (NULL,\'MySQLvs. YourSQL\', \'In the following database comparison ...\'),
- -> (NULL,\'MySQLSecurity\', \'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\');
- +----+-------------------+------------------------------------------+
- | id | title | body |
- +----+-------------------+------------------------------------------+
- | 5 |MySQLvs. YourSQL | In the following database comparison ... |
- | 1 |MySQLTutorial | DBMS stands for DataBase ... |
- +----+-------------------+------------------------------------------+
- 2 rows in set
【编辑推荐】