如何在MySQL中使用RegExp搜索特定单词?

如何在MySQL中使用RegExp搜索特定单词?

首先让我们创建一个表 –

mysql> create table DemoTable
    -> (
    -> Title varchar(255)
    -> );
Query OK,0 rows affected (1.82 sec)

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

mysql> insert into DemoTable values('John is a good player');
Query OK,1 row affected (0.26 sec)
mysql> insert into DemoTable values('No,John Doe is not a good player');
Query OK,1 row affected (0.17 sec)
mysql> insert into DemoTable values('Yes,Adam Smith is play cricket match');
Query OK,1 row affected (0.28 sec)
mysql> alter table DemoTable add fulltext(Title);
Query OK,0 rows affected,1 warning (10.60 sec)
Records: 0 Duplicates: 0 Warnings: 1

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

mysql> select *from DemoTable;

这将产生以下输出 –

+--------------------------------------+
| Title                                |
+--------------------------------------+
| John is a good player                |
| No,John Doe is not a good player     |
| Yes,Adam Smith is play cricket match |
+--------------------------------------+
3 rows in set (0.00 sec)

以下是在MySQL中搜索特定单词的查询 –

mysql> select *from DemoTable
    -> where Title REGEXP '^.*John.*$';

这将产生以下输出 –

+----------------------------------+
| Title                            |
+----------------------------------+
| John is a good player            |
| No,John Doe is not a good player |
+----------------------------------+
2 rows in set (0.11 sec)

阅读更多:MySQL 教程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程