
- 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
Implement Conditional MySQL Query in a stored procedure?
For conditional MySQL query, you can use IF ELSE concept in stored procedure. Let us first create a table −
mysql> create table DemoTable1 ( Id int ); Query OK, 0 rows affected (0.62 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1 values(10); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable1 values(20); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1 values(30); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable1;
This will produce the following output −
+------+ | Id | +------+ | 10 | | 20 | | 30 | +------+ 3 rows in set (0.00 sec)
Following is the query to create second table.
mysql> create table DemoTable2 ( Id int ); Query OK, 0 rows affected (1.07 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable2 values(40); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable2 values(50); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable2 values(60); Query OK, 1 row affected (0.11 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable2;
This will produce the following output −
+------+ | Id | +------+ | 40 | | 50 | | 60 | +------+ 3 rows in set (0.00 sec)
Following is the query to create a stored procedure for conditional MySQL query.
mysql> DELIMITER // mysql> CREATE PROCEDURE Conditional_Query(id int) BEGIN IF id >=40 THEN Select * from DemoTable2; ELSE Select * from DemoTable1; END IF; END // Query OK, 0 rows affected (0.21 sec) mysql> DELIMITER ;
Now you can call the stored procedure with the help of CALL command −
mysql> call Conditional_Query(100);
This will produce the following output −
+------+ | Id | +------+ | 40 | | 50 | | 60 | +------+ 3 rows in set (0.00 sec) Query OK, 0 rows affected (0.03 sec)
Call again and pass a different value −
mysql> call Conditional_Query(13);
This will produce the following output −
+------+ | Id | +------+ | 10 | | 20 | | 30 | +------+ 3 rows in set (0.00 sec) Query OK, 0 rows affected (0.02 sec)
- Related Articles
- Implement DELETE query in MySQL stored procedure
- Implement Dynamic SQL query inside a MySQL stored procedure?
- Implement If else in stored procedure in MySQL?
- How to correctly implement conditions in MySQL stored procedure?
- How to correctly implement END IF statement in a MySQL Stored Procedure?
- Conditional WHERE clause in MySQL stored procedure to set a custom value for NULL values
- Get database name from a query implemented in a MySQL Stored Procedure?
- MySQL Sum Query with IF Condition using Stored Procedure
- How can a MySQL stored procedure call another MySQL stored procedure inside it?
- Set conditions in a MySQL stored procedure
- MySQL stored procedure return value?
- MySQL stored-procedure: out parameter?
- Create a stored procedure with delimiter in MySQL
- Perform mathematical operations in a MySQL Stored Procedure?
- Create a MySQL stored procedure that counts the number of rows gets affected by MySQL query?

Advertisements