
- 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
Query for implementing MySQL LIKE as MySQL IN?
To implement a query like MySQL IN(), you need to use COUNT(), IF() along with LIKE operator. Let us first create a table −
mysql> create table DemoTable -> ( -> Subject varchar(80) -> ); Query OK, 0 rows affected (0.58 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('MySQLMongoDB'); Query OK, 1 row affected (0.86 sec) mysql> insert into DemoTable values('MySQL'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('JavaMySQL'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('MongoDB'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('Java'); Query OK, 1 row affected (0.19 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+--------------+ | Subject | +--------------+ | MySQLMongoDB | | MySQL | | JavaMySQL | | MongoDB | | Java | +--------------+ 5 rows in set (0.00 sec)
Here is the query for implementing LIKE as IN() −
mysql> select -> count(if(Subject like '%MySQL%',1,NULL)) as MySQLCount, -> count(if(Subject like '%Java%',1,NULL)) as JavaCount, -> count(if(Subject like '%MongoDB%',1,NULL)) as MongoDBCount -> from DemoTable;
This will produce the following output −
+------------+-----------+--------------+ | MySQLCount | JavaCount | MongoDBCount | +------------+-----------+--------------+ | 3 | 2 | 2 | +------------+-----------+--------------+ 1 row in set (0.00 sec)
- Related Articles
- MySQL LIKE query with dynamic array?
- Using LIKE clause twice in a MySQL query
- MySQL query for sorting on a columns partial value like number in “John_120 “
- MySQL query for text search with LIKE and OR to fetch records
- Can we use “LIKE concat()” in a MySQL query?
- Implement multiple LIKE operators in a single MySQL query
- How to treat MySQL longtext as integer in MySQL query?
- How to display the storage engine while implementing JDBC - MySQL CONNECTION query?
- Implement SELECT LIKE and CHAR_LENGTH() in a single MySQL query
- MySQL LIKE IN()?
- Use LIKE % to fetch multiple values in a single MySQL query
- Find the records with % character in a LIKE query with MySQL
- Using LIKE for two where clauses in MySQL?
- JavaScript - Find keys for the matched values as like query in SQL
- Combine columns before matching it with LIKE in a single query in MySQL?

Advertisements