mysql fulltext

本文介绍如何使用MySQL MyISAM引擎实现全文搜索功能。通过创建包含全文索引的表并进行查询,演示了如何根据相关度排序检索结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

只有MyISAM引擎支持 

create table articles(

 id int unsigned auto_increment not null primary key,
 title varchar(200),
 body text,
 fulltext(title,body)
 )ENGINE=MyISAM default charset=utf8;


 mysql>  create table articles(
    ->  id int unsigned auto_increment not null primary key,
    ->  title varchar(200),
    ->  body text,
    ->  fulltext(title,body)
    ->  )ENGINE=MyISAM default charset=utf8;
Query OK, 0 rows affected (0.07 sec)


mysql> desc articles;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| title | varchar(200)     | YES  | MUL | NULL    |                |
| body  | text             | YES  |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+
3 rows in set (0.10 sec)


mysql> select title,body, match(title,body) against('mysql') as score  from arti
cles ;
+------------------+--------------+--------------------+
| title            | body         | score              |
+------------------+--------------+--------------------+
| php              | lamp php     |                  0 |
| mysql guide      | book         | 0.6700310707092285 |
| optimizing mysql | in this page | 0.6700310707092285 |
| php security     | function     |                  0 |
| c++              | oop          |                  0 |
| java             | java web     |                  0 |
+------------------+--------------+--------------------+


mysql> select title,body, match(title,body) against('mysql') as score  from arti
cles where match(title,body) against('mysql');
+------------------+--------------+--------------------+
| title            | body         | score              |
+------------------+--------------+--------------------+
| mysql guide      | book         | 0.6700310707092285 |
| optimizing mysql | in this page | 0.6700310707092285 |
+------------------+--------------+--------------------+
2 rows in set (0.00 sec)


mysql> insert into articles(title,body)values('this is mysql mysql','mysql');
Query OK, 1 row affected (0.00 sec)


mysql> select title,body, match(title,body) against('mysql') as score  from arti
cles ;
+---------------------+--------------+---------------------+
| title               | body         | score               |
+---------------------+--------------+---------------------+
| php                 | lamp php     |                   0 |
| mysql guide         | book         |  0.2780880331993103 |
| optimizing mysql    | in this page |  0.2780880331993103 |
| php security        | function     |                   0 |
| c++                 | oop          |                   0 |
| java                | java web     |                   0 |
| this is mysql mysql | mysql        | 0.28441134095191956 |
+---------------------+--------------+---------------------+
7 rows in set (0.00 sec)


mysql> select title,body, match(title,body) against('mysql') as score  from arti
cles where match(title,body) against('mysql') ;
+---------------------+--------------+---------------------+
| title               | body         | score               |
+---------------------+--------------+---------------------+
| this is mysql mysql | mysql        | 0.28441134095191956 |
| mysql guide         | book         |  0.2780880331993103 |
| optimizing mysql    | in this page |  0.2780880331993103 |
+---------------------+--------------+---------------------+
有此可见 match 返回的是个相关度的值,按高到低排序,为0不相关,不显示



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值