Search record with a specific value in MySQL using LIKE or REGEXP?

MySQLMySQLi Database

Let us first create a table −

mysql> create table DemoTable651 (Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,Value varchar(100));
Query OK, 0 rows affected (0.59 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable651(Value) values('Title');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable651(Value) values('course');
Query OK, 1 row affected (0.26 sec)
mysql> insert into DemoTable651(Value) values('subject');
Query OK, 1 row affected (0.20 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable651;

This will produce the following output −

+----+---------+
| Id | Value   |
+----+---------+
|  1 | Title   |
|  2 | course  |
|  3 | subject |
+----+---------+
3 rows in set (0.00 sec)

Following is the query to search a specific value using LIKE −

mysql> select *from DemoTable651 where Value LIKE 'course';

This will produce the following output −

+----+--------+
| Id | Value  |
+----+--------+
|  2 | course |
+----+--------+
1 row in set (0.00 sec)

Following is the query to search a specific value using REGEXP −

mysql> select *from DemoTable651 where Value REGEXP 'course';

This will produce the following output −

+----+--------+
| Id | Value  |
+----+--------+
| 2  | course |
+----+--------+
1 row in set (0.00 sec)
raja
Published on 23-Aug-2019 11:33:17
Advertisements