SELECT note_text FROM productnotes WHERE Match(note_text) Against('rabbit');
SELECT notet_text FROM productnotes WHERE note_text LIKE '%rabbit%';
SELECT Match(note_text) Against('rabbit') FROM productnotes;
SELECT Match(note_text) Against('rabbit') AS rank FROM productnotes;
SELECT note_text,Match(note_text) Against('rabbit') AS rank FROM productnotes;
SELECT note_text FROM productnotes WHERE Match(note_text) Against('anvils');
SELECT note_text FROM productnotes WHERE Match(note_text) Against('anvils' WITH QUERY EXPANSION);
SELECT note_text FROM productnotes WHERE Match(note_text) Against('heavy');
SELECT note_text FROM productnotes WHERE Match(note_text) Against('heavy' IN BOOLEAN MODE);
SELECT note_text FROM productnotes WHERE Match(note_text) Against('heavy -rope*' IN BOOLEAN MODE);
SELECT note_text FROM productnotes WHERE Match(note_text) Against('+rabbit +bait' IN BOOLEAN MODE);
SELECt note_text FROM productnotes WHERE Match(note_text) Against('rabbit bait');
SELECT note_text FROM productnotes WHERE Match(note_text) Against('rabbit bait' IN BOOLEAN MODE);
SELECT note_Text FROM productnotes WHERE Match(note_text) Against('"rabbit bait"' IN BOOLEAN MODE);
SELECT note_text FROM productnotes WHERE Match(note_text) Against('>rabbit <carrot' IN BOOLEAN MODE);
SELECT note_text FROM productnotes WHERE Match(note_text) Against('+safe +(<combination)' IN BOOLEAN MODE);
关于全文本搜索(不是查询所有行,而是使用索引)
mysql支持几种基本的数据库引擎,比如MyISAM,InnoDB。
并不是所有的数据库引擎都支持全文本搜索,MyISAM支持全文本搜索,InnoDB不支持全文本搜索。
CREATE TABLE productnotes(
note_id int NOT NULL AUTO_INCREMENT,
prod_id char(10) NOT NULL,
note_date datetime NOT NULL,
note_text text NULL,
PRIMARY KEY(note_id),
FULLTEXT(note_text)
)ENGINE=MyISAM;
FULLTEXT指定note_text为索引列
SELECT note_text FROM productnotes WHERE Match(note_text) Against('rabbit');
Match 指定要搜索的列,Against指定搜索文本。
传递给Match的值必须与FULLTEXT中定义的相同。
如果FULLTEXT中定义了多个列,则Match必须列出它们,且次序相同。
全文本搜索会对搜索结果进行排序,具有高等级的行会先被返回,因为它很可能是你想要的行。
SELECT Match(note_text) Against('rabbit') FROM productnotes;
SELECT Match(note_text) Against('rabbit') AS rank FROM productnotes;
SELECT note_text,Match(note_text) Against('rabbit') AS rank FROM productnotes;
关于查询扩展:
SELECT note_text FROM productnotes WHERE Match(note_text) Against('anvils');
SELECT note_text FROM productnotes WHERE Match(note_text) Against('anvils' WITH QUERY EXPANSION);
关于布尔文本搜索(不会按照等级值降序排序)
布尔操作符包括:
+:包含
-:不包含
>:包含,且增加等级值
< :包含,且减少等级值
SELECT note_text FROM productnotes WHERE Match(note_text) Against('heavy');
SELECT note_text FROM productnotes WHERE Match(note_text) Against('heavy' IN BOOLEAN MODE);
------>包含字符串'heavy'
SELECT note_text FROM productnotes WHERE Match(note_text) Against('heavy -rope*' IN BOOLEAN MODE);
------>包含字符串'heavy',但不包含以'rope'为开头的字符串。(某行包含'heavy',但是也包含以‘rope’为开头的字符串,则不返回该行)
SELECT note_text FROM productnotes WHERE Match(note_text) Against('rabbit bait');
------>包含'rabbit'或'bait'其中任意一个就行
SELECT note_text FROM productnotes WHERE Match(note_text) Against('rabbit bait' IN BOOLEAN MODE);
------>包含'rabbit'或'bait'其中任意一个就行
SELECT note_text FROM productnotes WHERE Match(note_text) Against('+rabbit +bait' IN BOOLEAN MODE);
------>包含'rabbit'和'bait'两个字符串
SELECT note_text FROM productnotes WHERE Match(note_text) Against('"rabbit bait"' IN BOOLEAN MODE);
------>包含字符串'rabbit bait'
SELECT note_text FROM productnotes WHERE Match(note_text) Against('>rabbit <carrot' IN BOOLEAN MODE);
------>包含 'rabbit'或'carrot'其中任意一个,且rabbit的等级高,carrot的等级低
SELECT note_text FROM productnotes WHERE Match(note_text) Against('+safe +(<combination)' IN BOOLEAN MODE);
------>包含'safe'和'combination',且combination的等级低