
- 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
How do I return multiple results in a MySQL subquery with IN()?
In MySQL, you can easily return multiple results, but also achieve this with subquery using IN(). Let us first create a table −
mysql> create table DemoTable1317 -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(20) -> ); Query OK, 0 rows affected (0.49 sec)
Insert some records in the table using insert command
mysql> insert into DemoTable1317(Name) values('Chris Brown'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1317(Name) values('John Doe'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1317(Name) values('Adam Smith'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1317(Name) values('John Smith'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1317(Name) values('David Miller'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1317(Name) values('Carol Taylor'); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable1317;
Output
+----+--------------+ | Id | Name | +----+--------------+ | 1 | Chris Brown | | 2 | John Doe | | 3 | Adam Smith | | 4 | John Smith | | 5 | David Miller | | 6 | Carol Taylor | +----+--------------+ 6 rows in set (0.00 sec)
Following is the query to return multiple results in a MySQL subquery −
mysql> select Name from DemoTable1317 -> where Id IN ( select Id from DemoTable1317);
Output
+--------------+ | Name | +--------------+ | Chris Brown | | John Doe | | Adam Smith | | John Smith | | David Miller | | Carol Taylor | +--------------+ 6 rows in set (0.00 sec)
- Related Articles
- How do I insert multiple values in a column with a single MySQL query?
- How can I use MySQL subquery as a table in FROM clause?
- How do we return multiple values in Python?
- How can we create a MySQL view with a subquery?
- How can we use a MySQL subquery with INSERT statement?
- How can we use a MySQL subquery with FROM clause?
- How to correctly enclose subquery in MySQL?
- How do I search for names starting with A in MySQL?
- How do I return a boolean from AsyncTask in Android?
- How do I replace “+”(plus sign) with SPACE in MySQL?
- How do I create a view in MySQL?
- How do I loop through a JSON file with multiple keys/sub-keys in Python?
- How do I return a document with filtered sub-documents using Mongo?
- Subquery to exclude a particular row in MySQL
- How do I remove multiple elements from a list in Java?

Advertisements