
- 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 to implement WHILE LOOP with IF STATEMENT MySQL?
The following is an example to implement MySQL WHILE LOOP with IF statement. We are using in a stored procedure
The following is the query to create our stored procedure:
mysql> DELIMITER // mysql> create procedure sp_getDaysDemo() -> BEGIN -> SELECT MONTH(CURDATE()) INTO @current_month; -> SELECT MONTHNAME(CURDATE()) INTO @current_monthname; -> SELECT DAY(LAST_DAY(CURDATE())) INTO @total_numberofdays; -> SELECT CAST(DATE_FORMAT(NOW() ,'%Y-%m-01') as DATE)INTO @check_weekday; -> SELECT DAY(@check_weekday) INTO @check_day; -> SET @count_days = 0; -> SET @workdays = 0; -> -> WHILE(@count_days < @total_numberofdays) DO -> IF (WEEKDAY(@check_weekday) < 5) THEN -> SET @workdays = @workdays+1; -> END IF; -> SET @count_days = @count_days+1; -> SELECT ADDDATE(@check_weekday, INTERVAL 1 DAY) INTO @check_weekday; -> END WHILE; -> -> select @current_month,@current_monthname,@total_numberofdays,@check_weekday,@check_day; -> END -> -> // Query OK, 0 rows affected (0.24 sec) mysql> delimiter ;
Call the stored procedure using CALL command. The syntax is as follows:
CALL yourStoredProcedureName();
Now you can call the stored procedure using the following query:
mysql> call sp_getDaysDemo();
The following is the output:
+----------------+--------------------+---------------------+----------------+------------+ | @current_month | @current_monthname | @total_numberofdays | @check_weekday | @check_day | +----------------+--------------------+---------------------+----------------+------------+ | 1 | January | 31 | 2019-02-01 | 1 | +----------------+--------------------+---------------------+----------------+------------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.02 sec)
- Related Articles
- How to use PowerShell Break statement with the While Loop?
- How MySQL WHILE loop statement can be used in stored procedure?
- How to correctly implement END IF statement in a MySQL Stored Procedure?
- Implement MySQL CASE statement with WHEN clause
- What is while loop statement in JavaScript?
- What is do...while loop statement in JavaScript?
- How do we use continue statement in a while loop in C#?
- How do we use a break statement in while loop in C#?
- Java while loop with Examples
- How to use MySQL CASE statement while using UPDATE Query?
- How to use a select statement while updating in MySQL?
- Java continue statement with Loop
- How to convert a Python for loop to while loop?
- Python - How to convert this while loop to for loop?
- How to use PowerShell break statement with the For loop?

Advertisements