How to search specific word in MySQL with RegExp?

MySQLMySQLi Database

Let us first create a table −

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

Insert some records in the table using insert command −

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

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+--------------------------------------+
| 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)

Here is the query to search specific word in MySQL −

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

This will produce the following output −

+----------------------------------+
| Title                            |
+----------------------------------+
| John is a good player            |
| No,John Doe is not a good player |
+----------------------------------+
2 rows in set (0.11 sec)
raja
Published on 13-Dec-2019 09:59:43
Advertisements