
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
Using OR with Multiple MySQL LIKE Statements
You can OR two like statements using the following syntax −
SELECT *FROM yourTableName WHERE (yourColumnName like '%yourValue1%' OR yourColumnNamelike '%yourValue2%') AND yourColumnName = yourValue;
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table ORLikeDemo -> ( -> Id int not null auto_increment, -> FirstName varchar(15), -> LastName varchar(15), -> Primary Key(Id) -> ); Query OK, 0 rows affected (1.19 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into ORLikeDemo(FirstName,LastName) values('John','Smith'); Query OK, 1 row affected (0.13 sec) mysql> insert into ORLikeDemo(FirstName,LastName) values('Carol','Taylor'); Query OK, 1 row affected (0.23 sec) mysql> insert into ORLikeDemo(FirstName,LastName) values('John','Doe'); Query OK, 1 row affected (0.11 sec) mysql> insert into ORLikeDemo(FirstName,LastName) values('Bob','Jones'); Query OK, 1 row affected (0.20 sec) mysql> insert into ORLikeDemo(FirstName,LastName) values('David','Miller'); Query OK, 1 row affected (0.17 sec) mysql> insert into ORLikeDemo(FirstName,LastName) values('Mike','Williams'); Query OK, 1 row affected (0.11 sec) mysql> insert into ORLikeDemo(FirstName,LastName) values('Sam','Taylor'); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from ORLikeDemo;
The following is the output −
+----+-----------+----------+ | Id | FirstName | LastName | +----+-----------+----------+ | 1 | John | Smith | | 2 | Carol | Taylor | | 3 | John | Doe | | 4 | Bob | Jones | | 5 | David | Miller | | 6 | Mike | Williams | | 7 | Sam | Taylor | +----+-----------+----------+ 7 rows in set (0.00 sec)
Here is the query to use two LIKE with OR operator −
mysql> select *from ORLikeDemo where (LastName like '%Smith%' OR LastName like'%Doe%') AND Id = 3;
The following is the output −
+----+-----------+----------+ | Id | FirstName | LastName | +----+-----------+----------+ | 3 | John | Doe | +----+-----------+----------+ 1 row in set (0.03 sec)
Advertisements