
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
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)
- Related Questions & Answers
- Can we use NOT and AND together in MongoDB?
- Can I use MySQL COUNT() and DISTINCT together?
- Can we combine MySQL IN AND LIKE operators?
- Can we use “LIKE concat()” in a MySQL query?
- Can we use WHERE, AND & OR in a single MySQL query?
- Can we use str_replace in MySQL?
- Use MySQL LIKE and NOT LIKE to display similar results?
- Can we use MySQL GROUP BY clause with multiple columns like MySQL DISTINCT clause is used?
- How can we create and use a MySQL trigger?
- How can we create and use ENUM columns in MySQL?
- How can we use MySQL SUM() function?
- Can we use ADD and CHANGE with ALTER Statement in MySQL?
- How can we use prepared statements in MySQL?
- How can we use nested transactions in MySQL?
- Can we use ORDER BY NULL in MySQL?
Advertisements