在 MySQL 中使用 MATCH 和 AGAINST 选择包含特定列中的字符串的行

mysqlmysqli database更新于 2024/10/12 16:44:17

首先我们创建一个表 −

mysql> create table DemoTable1833
     (
     Name varchar(20)
     );
Query OK, 0 rows affected (0.00 sec)

修改表 −

Mysql> alter table DemoTable1833 ADD FULLTEXT(Name);
Query OK, 0 rows affected, 1 warning (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 1

使用 insert 命令在表中插入一些记录 −

mysql> insert into DemoTable1833 values('John Doe');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1833 values('Adam Smith');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1833 values('Chris Brown');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1833 values('John Smith');
Query OK, 1 row affected (0.00 sec)

使用 select 语句显示表中的所有记录 −

mysql> select * from DemoTable1833;

这将产生以下输出 −

+-------------+
| Name        |
+-------------+
| John Doe    |
| Adam Smith  |
| Chris Brown |
| John Smith  |
+-------------+
4 rows in set (0.00 sec)

以下查询用于选择特定列中包含字符串的行

mysql> select * from DemoTable1833 where MATCH(Name) AGAINST('+John Smith+') ;

这将产生以下输出 −

+------------+
| Name       |
+------------+
| John Smith |
| John Doe   |
| Adam Smith |
+------------+
3 rows in set (0.00 sec)

相关文章


有用资源