You can use LIKE with the OR operator, which works the same as the IN operator.
Let’s see the syntax for both cases -
select *from yourTableName where yourColumnName Like ‘Value1’ or yourColumnName Like ‘Value2’ or yourColumnName Like ‘Value3’ . . . N
select *from yourTableName where IN(value1,value2,value3,.....N);
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)
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)
Use the select statement to display all records in the table. 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 using single Like and OR operators-
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)
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)
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!