×

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

MySQL Union

MySQL contains the same categories of data in a different table. Sometimes, you require multiple tables to collect data together. MySQL union is a function to combine two table's data...

5 minutes read.

MySQL Date and Time function

The date function displays day, year, month, time, and current date. It shows the date and time as per the requirement of the applications. The data either store on the...

8 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 UPDATE JOIN

The UPDATE JOIN is a MySQL statement used to perform cross-table updates that means we can update one table using another table with the JOIN clause condition. The "update join"...

5 minutes read.

MySQL DATE_ADD() function

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

3 minutes read.

MySQL PERIOD_DIFF() function

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

2 minutes read.

MySQL String Function

The string function is used to maintain and operate string values. This function modifies the string data as per function. We can do the concatenation, conversion, removal and replacing the...

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

MySQL Clauses MySQL system clauses are keywords or statements to handle information. It helps to operate a group of the data and apply it to require conditions. The clauses apply conditions...

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

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 AVG function

This function works on numerical data type values. The average function shows the average value of the data set. If an average function returns a null value, then the row...

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