×

MySQL Control flow function

The control flow function uses values or operands for logical operations. These functions can works on single and multiple conditions. The control flow function is based on the Boolean expression. It controls and manages table data as per requirement.  The Boolean expressions mean 0 (null) value and 1(not null) value. The below table shows the type of the control flow function.

MySQL IF statementThe IF statement shows the true condition of the control flow function.
MySQL IFNULL statementThe IFNULL statement shows null or not null of the data.
MySQL NULLIF statementThe NULLIF statement works on the not null value of the data.
MySQL CASE statementThis statement works on multiple conditions.

MySQL IF statement

The first condition is necessary to display the result. The other condition is optional in the "IF" statement. The IF statement shows the true condition of the control flow function.

Example of the IF statement

Example: the "IF" statement with true condition example shows below.

Execute the below statement that shows the first condition is true.

mysql> SELECT IF(7 = 7, 'equal', 'not_equal');

Output

MySQL control flow function

 The above output image shows equal output because when the condition becomes true, the query shows the first expression like equal.

Example: the "IF" statement with false condition example shows below.

Execute the below statement that shows the first condition is false.

mysql> SELECT IF(7 = 13,'equal','not_equal');

Output

MySQL control flow function

The above output image shows not equal output because when the condition becomes false, the query shows a second expression like not equal.

MySQL IFNULL statement

The IFNULL statement shows the null or not null result of the control flow function. If the first condition is not null, it returns the first condition. If the first condition is null, then it returns the second condition. This statement contains string and numeric values.

Examples of the IFNULL statement

The "IFNULL" statement with true condition example shows below.

Execute the below query to understand the working procedure of numerical values and their output.

mysql> SELECT IFNULL(1, 0);

Output

MySQL control flow function

The above image shows the first value of the condition. If the first value is not null, then MySQL displays these values. The above query shows one output.

MySQL NULLIF statement

The IF statement shows the true condition of the control flow function. The first condition shows equal to the second condition, then it returns the first condition value. The first condition shows the "not equal" to the second condition then the query returns the second condition value.

Examples of the NULLIF statement

Example: the "NULLIF" statement with true condition example shows below.

Execute the below query to understand the working procedure of NULLIF and their output.

mysql> SELECT NULLIF(1, 0);

Output

MySQL control flow function

The above image shows the first value of the condition. If the first value shows the "not equal" to the second value, MySQL displays the first value.

Example: the "NULLIF" statement example shows below.

mysql> SELECT NULLIF (1, 1);

Output

MySQL control flow function

The above returns the above output. If the first value is equal to the second value, then MySQL displays the NULL value. The above query shows the NULL output.

MySQL CASE statement

The case statement works as "if else" and "if then else" statements. This statement works on multiple conditions in a single query. If the first condition is true, then returns the first condition. If the first condition is false, then it uses other conditions.

Example: the "CASE" statement with numerical and string value example shows below.

Execute the below query to get the required value using a case statement. Here, you can use string values with the "CASE" and their conditions.

mysql> SELECT CASE 'MON' WHEN 'SUN' THEN 'SUNDAY' WHEN 'MON' THEN 'MONDAY' ELSE 'OTHERS' END;

Output

MySQL control flow function

The above image displays string values with given conditions. The first row of the table shows the case statement with value. The second row displays the output value. The output table shows the "MONDAY" value.


Related Topics

MySQL vs Oracle

What is MySQL? The open-source MySQL relational database management system is a vital software component for web-based applications. As the data is saved and sent over the internet, databases and related...

6 minutes read.

MySQL Stored Procedure

MySQL creates the "stored procedure" function to operate database information. You can use parameters, blocks, and statements to create a new procedure. The procedure requires a database table to use...

3 minutes read.

MySQL LOCATE() function

In this context, we will learn how we can use the MySQL LOCATE() function with proper syntax and good examples. Introduction of MySQL LOCATE() function LOCATE() function in MySQL is used for...

3 minutes read.

MySQL Descending Index

Descending Index It is a type of index that stores key values in descending order. This query improves the performance of an index query. MySQL system displays the index as per...

3 minutes read.

MySQL Show index

This query helps to access and retrieve table information. This syntax displays the required index or all indexes of the table. It also displays the index type as per the...

3 minutes read.

MySQL Command line client Basic Queries

MySQL – command line client Basic Queries There are many queries to work with the MySQL command-line client. Today, we will explore MySQL basic queries and their function. The MySQL query...

8 minutes read.

MySQL Table

Introduction MySQL table is an essential part of the system. MySQL table stores data using index, rows, and columns. It accesses data from the server quickly because of the table—this table...

22 minutes read.

MySQL FORMAT() function

In this context, we will learn how we can use the MySQL FORMAT() function with proper syntax and good examples. Introduction of MySQL FORMAT() function This function in MySQL helps to format...

2 minutes read.

MySQL MONTHNAME() function

In this context, we will learn how we can use the MySQL MONTHNAME() Function with proper syntax and good examples. Introduction of MySQL MONTHNAME() function In MySQL the MONTHNAME() Function is used...

3 minutes read.

MySQL Math Function

The math function operates numerical values and displays required number data. The math function finds out sin, cos, tan values. Here, you find out the absolute value, reminder, and logarithmic...

6 minutes read.

MySQL CONCAT() function

In this context, we will learn how we can use the MySQL CONCAT() function with proper syntax and good examples. Introduction of MySQL CONCAT() function By using the MySQL CONCAT function, we...

3 minutes read.

MySQL RIGHT JOIN

MySQL right join links two tables with each other. The left table column connects with the complete right side table. Each row of the right table tries to connect with...

4 minutes read.

MySQL Table Query

MySQL table query A database stored a lot of data and divided them into different relations known as tables. Each database can contain more than one table. These tables are created...

7 minutes read.

MySQL EQUI JOIN

An equijoin is an operation that combines multiple tables based on equality or matching column values in the associated tables. This operation links more than two tables based on a...

5 minutes read.

MySQL SOUNDEX() function

In this context, we will learn how we can use the MySQL SOUNDEX() function with proper syntax and good examples. Introduction of MySQL SOUNDEX() function SOUNDEX() function in MySQL is used to...

3 minutes read.

MySQL function

MySQL function is a piece of program to perform some specific task. Here we pass a parameter and then return a single value. MySQL function mainly supports string, numeric, conditional,...

9 minutes read.

MySQL Tutorial

In this tutorial, you will get information about the MySQL management system. This tutorial will cover the basic and advanced level MySQL concepts with examples that will help you become...

5 minutes read.

MySQL COUNT function

The count function returns the number of rows in the table. This function shows either the entire rows count or the required row count of the table. It determines the...

5 minutes read.

MySQL RPAD() function

In this context, we will learn how we can use the MySQL RPAD() function with proper syntax and good examples. Introduction of MySQL RPAD() function RPAD() function in MySQL is used to...

2 minutes read.

MySQL database queries

MySQL database queries Database queries help to create, use, show and, delete the database. A Database is a collection of several tables and data. The database uses SQL language to perform...

3 minutes read.