- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Can we use LIKE and OR together in MySql?
You can use LIKE with OR operator which works same as IN operator.
Let us see the syntax for both the cases −
Case 1 − Using Like with OR operator.
select *from yourTableName where yourColumnName Like ‘Value1’ or yourColumnName Like ‘Value2’ or yourColumnName Like ‘Value3’ . . . N
Case 2 − Using IN operator.
select *from yourTableName where IN(value1,value2,value3,.....N);
To understand both the syntaxes, let us create a table. The query to create a table is as follows −
mysql> create table LikeDemo −> ( −> Id varchar(20) −> ); Query OK, 0 rows affected (0.58 sec)
Now you can insert records in the table with the help of insert statement. The query is as follows −
mysql> insert into LikeDemo values('John123'); Query OK, 1 row affected (0.22 sec) mysql> insert into LikeDemo values('Smith205'); Query OK, 1 row affected (0.18 sec) mysql> insert into LikeDemo values('Bob999'); Query OK, 1 row affected (0.18 sec) mysql> insert into LikeDemo values('Carol9091'); Query OK, 1 row affected (0.17 sec) mysql> insert into LikeDemo values('Johnson2222'); Query OK, 1 row affected (0.15 sec) mysql> insert into LikeDemo values('David2345'); Query OK, 1 row affected (0.21 sec)
Display all records from the table with the help of select statement. The query is as follows −
mysql> select *from LikeDemo;
The following is the output −
+-------------+ | Id | +-------------+ | John123 | | Smith205 | | Bob999 | | Carol9091 | | Johnson2222 | | David2345 | +-------------+ 6 rows in set (0.00 sec)
The following is the query to use single Like with OR operator −
Case 1 − Using Like with OR operator
mysql> select *from LikeDemo where Id Like 'John123%' or Id Like 'Carol9091%' or Id Like 'David2345%';
The following is the output −
+-----------+ | Id | +-----------+ | John123 | | Carol9091 | | David2345 | +-----------+ 3 rows in set (0.00 sec)
Case 2 − Using IN operator
The query is as follows −
mysql> select *from LikeDemo where Id in('John123','Carol9091', 'David2345');
The following is the output −
+-----------+ | Id | +-----------+ | John123 | | Carol9091 | | David2345 | +-----------+ 3 rows in set (0.04 sec)
Advertisements