
- 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
Working with WHERE IN() in a MySQL Stored Procedure
Let us first create a table −
mysql> create table DemoTable -> ( -> Id int, -> Name varchar(20) -> ); Query OK, 0 rows affected (0.69 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(100,'Chris'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(101,'Bob'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(102,'David'); Query OK, 1 row affected (0.18 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable;
This will produce the following output −
+------+-------+ | Id | Name | +------+-------+ | 100 | Chris | | 101 | Bob | | 102 | David | +------+-------+ 3 rows in set (0.00 sec)
Here is the query to create a stored procedure to use WHERE IN() −
mysql> DELIMITER // mysql> CREATE PROCEDURE whereInDemo(in input varchar(100)) -> BEGIN -> set @Query = 'select Name from DemoTable '; -> set @Query = CONCAT(@Query,' where Id IN (',`input`,')'); -> prepare stmt from @Query; -> execute stmt; -> deallocate prepare stmt; -> END // Query OK, 0 rows affected (0.23 sec) mysql> DELIMITER ;
Now you can call stored procedure using CALL command −
mysql> call whereInDemo('100,102');
This will produce the following output −
+-------+ | Name | +-------+ | Chris | | David | +-------+ 2 rows in set (0.04 sec) Query OK, 0 rows affected, 1 warning (0.07 sec)
- Related Articles
- MySQL Stored Procedure DEFINER=`root`@`%` is not working in localhost?
- Create a stored procedure with delimiter in MySQL
- Set conditions in a MySQL stored procedure
- How can a MySQL stored procedure call another MySQL stored procedure inside it?
- Create variables in MySQL stored procedure with DECLARE keyword
- Implement Conditional MySQL Query in a stored procedure?
- Perform mathematical operations in a MySQL Stored Procedure?
- Conditional WHERE clause in MySQL stored procedure to set a custom value for NULL values
- How can I create MySQL stored procedure with IN parameter?
- Insert data in a table in MySQL stored procedure?
- Insert values in two tables with a single stored procedure call in MySQL
- View stored procedure/function definition in MySQL?
- Implement DELETE query in MySQL stored procedure
- How to loop thrugh a stored procedure in MySQL?
- Display table records from a stored procedure in MySQL

Advertisements