Home > Database > Mysql Tutorial > body text

Can we use LIKE and OR together in MySql?

WBOY
Release: 2023-08-26 09:37:10
forward
1638 people have browsed it

Can we use LIKE and OR together in MySql?

You can use LIKE with the OR operator, which works the same as the IN operator.

Let’s see the syntax for both cases -

Case 1 - Use Like with the OR operator.

select *from yourTableName where yourColumnName Like ‘Value1’
or yourColumnName Like ‘Value2’
or yourColumnName Like ‘Value3’
.
.
.
N
Copy after login

Case 2 - Use IN operator.

select *from yourTableName where IN(value1,value2,value3,.....N);
Copy after login

To understand these two syntaxes, let us create a table. The query to create the table is as follows -

mysql> create table LikeDemo
−> (
−> Id varchar(20)
−> );
Query OK, 0 rows affected (0.58 sec)
Copy after login

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)
Copy after login

Use the select statement to display all records in the table. The query is as follows -

mysql> select *from LikeDemo;
Copy after login

The following is the output-

+-------------+
| Id          |
+-------------+
| John123     |
| Smith205    |
| Bob999      |
| Carol9091   |
| Johnson2222 |
| David2345   |
+-------------+
6 rows in set (0.00 sec)
Copy after login

The following is the query using single Like and OR operators-

Case 1 - Using Like and OR operators

mysql> select *from LikeDemo where Id Like 'John123%' or Id Like 'Carol9091%' or Id Like 'David2345%';
Copy after login

The following is the output-

+-----------+
| Id        |
+-----------+
| John123   |
| Carol9091 |
| David2345 |
+-----------+
3 rows in set (0.00 sec)
Copy after login

Case 2 - Using the IN operator

The query is as follows-

mysql> select *from LikeDemo where Id in('John123','Carol9091', 'David2345');
Copy after login

The following is the output-

+-----------+
| Id        |
+-----------+
| John123   |
| Carol9091 |
| David2345 |
+-----------+
3 rows in set (0.04 sec)
Copy after login

The above is the detailed content of Can we use LIKE and OR together in MySql?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template