您可以向文本字段添加全文索引,例如:
create table people (
id int unsigned auto_increment not null primary key,
name varchar(255),
fulltext(name)
);
插入一些测试值:
insert into people (name) values ('smith'),('smithers'),('wosmithling'),('smith-ponsonby');
然后使用MATCH()
运算符查询,例如,仅查询“smith”:
select * from people where match(name) against ('smith' in boolean mode);
+----+----------------+
| id | name |
+----+----------------+
| 1 | smith |
| 4 | smith-ponsonby |
+----+----------------+
用破折号:
select * from people where match (name) against ('smith-ponsonby' in boolean mode);
+----+----------------+
| id | name |
+----+----------------+
| 1 | smith |
| 4 | smith-ponsonby |
+----+----------------+
带空格:
select * from people where match (name) against ('smith ponsonby' in boolean mode);
+----+----------------+
| id | name |
+----+----------------+
| 1 | smith |
| 4 | smith-ponsonby |
+----+----------------+