
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL Query to Match Any of Two Strings from Column Values
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)
Advertisements