Syntax error Can we combine MySQL IN AND LIKE operators?

Can we combine MySQL IN AND LIKE operators?



Yes, we can combine IN and LIKE operators in MySQL using LIKE and OR operator. Let us first create a table −

mysql> create table DemoTable
-> (
-> SubjectTitle text
-> );
Query OK, 0 rows affected (0.68 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('Introduction To MySQL');
Query OK, 1 row affected (0.11 sec)

mysql> insert into DemoTable values('Java in Depth');
Query OK, 1 row affected (0.17 sec)

mysql> insert into DemoTable values('Coding with Python');
Query OK, 1 row affected (0.54 sec)

mysql> insert into DemoTable values('C++ in Depth');
Query OK, 1 row affected (0.17 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

This will produce the following output −

+-----------------------+
| SubjectTitle          |
+-----------------------+
| Introduction To MySQL |
| Java in Depth         |
| Coding with Python    |
| C++ in Depth          |
+-----------------------+
4 rows in set (0.00 sec)

Following is the query to use LIKE and OR in a single MySQL query −

mysql> select *from DemoTable where SubjectTitle LIKE '%MySQL%' OR SubjectTitle LIKE'C++%';

Output

This will produce the following output −

+-----------------------+
| SubjectTitle          |
+-----------------------+
| Introduction To MySQL |
| C++ in Depth          |
+-----------------------+
2 rows in set (0.00 sec)
Updated on: 2020-06-30T14:12:18+05:30

700 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements