×

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

find_in_set() function in MySQL

This Function is used for finding the position of a particular string from the list of strings. For suppose if the specified string is repeated multiple times, then this functions...

4 minutes read.

MySQL vs SQL

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 EXPORT_SET() function

In this context, we will learn how we can use the MySQL EXPORT_SET() function with proper syntax and good examples. Introduction of MySQL EXPORT_SET() function This function returns a string and shows...

3 minutes read.

MySQL DATEDIFF() function

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

3 minutes read.

MySQL TAN() Function

Basically, the TAN() function in MySQL is used to give back the tangent of a mentioned number.In any kind of right-angle triangle, the tangent of an angle is the length...

2 minutes read.

MySQL Create index

An index creation helps to make a row of the table unique. The index operates and handles table data quickly. MySQL index requires NOT NULL column constraint. The index column...

2 minutes read.

MySQL NOW() function

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

2 minutes read.

MySQL ISNULL function

This function is used to check the null value in table data. It returns the result in a Boolean form. If table data is null, then the output becomes 1....

2 minutes read.

MySQL Database Introduction

MySQL - Database Introduction: The database plays an essential role in MySQL for storing and modifying the data. The database creates and keeps multiple tables. The database organizes and manipulates...

8 minutes read.

MySQL ROUND() function

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

3 minutes read.

MySQL Truncate() Function

In this context, we will learn how we can use the MYSQL TRUNCATE function to truncate a number to a mentioned number of decimal places. Syntax of MySQL Truncate Function A number...

2 minutes read.

MySQL LN() function

In this context, we will learn how we can use the MySQL LN() function with proper syntax and good examples. Introduction of MySQL LN() function For evaluation of the natural logarithm of...

2 minutes read.

MySQL logical conditions

MySQL logical conditions Introduction MySQL handles data with clauses, operators, and conditions. The logical condition is used to compare information and returns the required output. This condition applies logic to MySQL expressions...

7 minutes read.

MySQL MAX function

The MAX function is a type of aggregation function that determines the maximum value of the table data. This function works on numerical data type values. If the table displays...

4 minutes read.

MySQL PERIOD_ADD() function

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

2 minutes read.

MySQL Aggregate function

The aggregation function works on the multiple values and returns single value output.  It makes the advanced and complex operation simple. The summation (SUM), MAX, AVG, MIN, COUNT functions are...

5 minutes read.

MySQL Index

An index is a widely used method to access table information quickly. MySQL requires an index for operating table data stored in rows and columns. It's an entry point of...

3 minutes read.

MySQL FROM_BASE64() function

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

2 minutes read.

MySQL CROSS JOIN

MySQL CROSS JOIN combines all possibilities of the two or more tables and returns the result that contains every row from all contributing tables. It links several columns from the...

4 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.