
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Can we implement 'LIKE' and ‘IN’ in a single MySQL query?
For more efficiency, use Regular Expression for the same task. Let us first create a table −
mysql> create table DemoTable -> ( -> Name varchar(30) -> ); Query OK, 0 rows affected (0.62 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+--------+ | Name | +--------+ | John | | Chris | | Robert | | David | +--------+ 4 rows in set (0.00 sec)
Here is the query to implement REGEXP for LIKE and IN alternative −
mysql> select *from DemoTable where Name REGEXP 'John|Robert|David';
This will produce the following output −
+--------+ | Name | +--------+ | John | | Robert | | David | +--------+ 3 rows in set (0.11 sec)
- Related Articles
- Implement SELECT LIKE and CHAR_LENGTH() in a single MySQL query
- Implement multiple LIKE operators in a single MySQL query
- Can we use “LIKE concat()” in a MySQL query?
- Can we use WHERE, AND & OR in a single MySQL query?
- Implement multiple COUNT() in a single MySQL query
- Implement MySQL LIMIT and OFFSET in a single query stating its difference
- Use LIKE % to fetch multiple values in a single MySQL query
- Can we update a row with the highest ID in a single MySQL query?
- Can we combine MySQL IN AND LIKE operators?
- Combine columns before matching it with LIKE in a single query in MySQL?
- How can we sort multiple columns in a single query?
- Can we use LIKE and OR together in MySql?
- How can we implement a MySQL transaction?
- Can we set a single value in MySQL SELECT IN()?
- Wrap around to first value and implement MySQL ORDER BY ASC and DESC in a single query

Advertisements