MySQL query for text search with LIKE and OR to fetch records


Let us first create a table −

mysql> create table DemoTable
(
   Subject text
);
Query OK, 0 rows affected (0.86 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('Introduction to MySQL');
Query OK, 1 row affected (0.31 sec)
mysql> insert into DemoTable values('Deep Dive using Java');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values('C in Depth');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values('Introduction to C++');
Query OK, 1 row affected (0.48 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-----------------------+
| Subject               |
+-----------------------+
| Introduction to MySQL |
| Deep Dive using Java  |
| C in Depth            |
| Introduction to C++   |
+-----------------------+
4 rows in set (0.00 sec)

Following is the query for text search with LIKE and OR −

mysql> select *from DemoTable where Subject LIKE '%MySQL%' OR Subject LIKE '%using%';

This will produce the following output −

+-----------------------+
| Subject               |
+-----------------------+
| Introduction to MySQL |
| Deep Dive using Java  |
+-----------------------+
2 rows in set (0.00 sec)
raja
Published on 10-Oct-2019 15:14:26
Advertisements