MySQL query to match any of the two strings from column values

MySQLMySQLi Database

For this, you can use LIKE operator with OR condition.

Let us first create a table −

mysql> create table DemoTable762 (Title text);
Query OK, 0 rows affected (0.54 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable762 values('Introduction to Java');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable762 values('MySQL is a RDBMS');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable762 values('Data Structure and Algorithm in Java');
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable762 values('Data Structure and Algorithm in C and C++');
Query OK, 1 row affected (0.18 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable762 ;

This will produce the following output -

+-------------------------------------------+
| Title                                     |
+-------------------------------------------+
| Introduction to Java                      |
| MySQL is a RDBMS                          |
| Data Structure and Algorithm in Java      |
| Data Structure and Algorithm in C and C++ |
+-------------------------------------------+
4 rows in set (0.00 sec)

Following is the query to match any of the two strings “MySQL” or “Algorithm” −

mysql> select *from DemoTable762 where Title LIKE '%MySQL%' or Title LIKE '%Algorithm%';

This will produce the following output -

+-------------------------------------------+
| Title                                     |
+-------------------------------------------+
| MySQL is a RDBMS                          |
| Data Structure and Algorithm in Java      |
| Data Structure and Algorithm in C and C++ |
+-------------------------------------------+
3 rows in set (0.00 sec)
raja
Published on 03-Sep-2019 11:51:10
Advertisements