×

MySQL IF statement

The IF statement shows the true condition of the control flow function. The first condition is necessary to fulfill the requirement. The other condition is optional in the "IF" statement. The "if statement" always displays Boolean expressions such as true and false.

Syntax

The "if statement" syntax shows below.

IF (condition1, condition2, condition3) 

A condition1 is required for the "IF" statement.

A condition2 and condition3 are optional for the "IF" statement.

Examples of the IF statement

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

Execute the below statement to check the given condition is true or not.

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

Output

MySQL IF statement

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 to check the first condition is true or false.

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

Output

MySQL IF statement

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

Example: the "IF" statement with table data example shows below.

This example contains the first condition with two output expressions.

mysql> SELECT topic_id, topic, 
IF(teaching_hour >= 15, "Complete", teaching_hour)
FROM arts;

Output

MySQL IF statement

The above image shows the "if" condition and its output statement. If the teaching_hour column is greater than equal to 15 values, then the teaching_hour column becomes complete. If the teaching_hour column is less than 15 values, then the teaching_hour column displays the value.

Example: the "IF" statement with the multiple conditions example shows below.

The below statement shows the first condition with two output expression.

mysql> SELECT 
topic_id, 
IF(topic = "journalism", "TRUE", topic), 
IF(teaching_hour > 15, "FULL", teaching_hour)
FROM arts
ORDER BY teaching_hour ASC;

Output

MySQL IF statement

Executing the statement will return the above output. If the column fulfills the condition, then the table column displays the true statement. If the column does not fulfill the condition, then the table column displays a false statement. Here table data display in an ascending order using the teaching_hour column.

Example: the "IF" statement with the "WHERE" clause example shows below.

This statement shows the first condition with two output expressions. Here, where column uses for topic_id column.

mysql> SELECT topic_id, topic, 
IF(teaching_hour > 15, "FULL", teaching_hour)
FROM arts
WHERE topic_id <= 3
ORDER BY teaching_hour DESC;

Output

MySQL IF statement

The statement returns the above result. If the teaching_hour column is greater than and equal to the "15", then the teaching_hour column becomes FULL. If the teaching_hour column is less than the "15", the teaching_hour column displays the value. This table data displays in descending order.

Example: Multiple "IF" statement with the multiple conditions example shows below.

mysql> SELECT 
topic_id, 
IF(topic = "writing", "TRUE", topic), 
IF(teaching_hour > 15, "FULL", teaching_hour)
FROM arts
WHERE topic_id < 3 OR teaching_hour = "FULL";

Output

MySQL IF statement

The statement returns the above result. If the given column fulfills the condition, then the table column displays the true statement. If the given column does not fulfill the condition, then the table column displays a false statement.


Related Topics

Clustered vs Non-Clustered Index

Clustered vs. Non-Clustered Index The difference between clustered and non-clustered index is the most famous question in the database-related interviews. Both indexes have the same physical structure and are stored as...

2 minutes read.

MySQL Operators

MySQL Operators MySQL operator needs advanced operation on the table and its data. This operator works with the "WHERE" clause. MySQL operators are a statement to modify information. It helps to...

13 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: Entity-Relationship Model

Entity-Relationship Model Entity-Relationship model or E R model is used to create a relationship between different attributes or entities. It describes the structure of the database with the help of the...

5 minutes read.

MySQL FIND_IN_SET() function

In this context, we will learn how we can use the MySQL MOD() function with proper syntax and good examples. Introduction of MySQL MOD() function MySQL provides a built-in string function called...

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

MySQL Queries MySQL supports SQL queries in the MySQL interface. These queries help to interact data with the application. MySQL uses create database, user database, create a table, truncate table, and...

13 minutes read.

MySQL RAND() function

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

3 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 variable

The MySQL variable is essential for storing data in the table. The variable declares data with a specific name or label to avoid confusion. This data label is used in...

6 minutes read.

MySQL DELETE JOIN

Sometimes join becomes complicated and unwanted. In such cases, we can delete the unwanted joins in the tables. You can delete inner join, left join, right join as per requirement....

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 Subquery

The subquery is a MySQL query used to manage data operations. Mainly subquery helps to retrieve data with the necessary condition. It creates a nested query with two different queries....

5 minutes read.

MySQL LEAST function

This statement displays the smallest value of the table. The LEAST function returns a null value when the table contains a null value. If the table contains all numerical values,...

2 minutes read.

MySQL SEC_TO_TIME() function

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

2 minutes read.

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 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 Character Length Function

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

2 minutes read.

MySQL MOD() function

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

3 minutes read.

MySQL FLOOR() function

In this context, we will learn how we can use the MySQL FLOOR() function with proper syntaxes and examples. Introduction of MySQL Floor() function: FLOOR() function in MySQL is used to return...

3 minutes read.