×

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

MySQL Advance function

The advance function operates numerical values, string values, and data types.  The advance function converts, displays, and compares given values as per requirement. Here, you find out database, table, and...

4 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 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 LENGTH() Function

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

2 minutes read.

MySQL Composite Index

Composite Index A composite index is an index that is used on multiple columns. MySQL management system manages multiple columns simultaneously. Therefore, the single index contains multiple columns in one query...

3 minutes read.

MySQL HEX() function

In this context, we will learn how we can use the MySQL HEX() function with proper syntax and good examples. Introduction of MySQL HEX() function For returning an equivalent hexadecimal string value...

3 minutes read.

MySQL POWER() function

In this context, we will learn how we can use the MySQL POWER() function with proper syntax and good examples. Introduction of MySQL POWER() function The value of a number raised to...

3 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 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 LOG10() function

In this context, we will learn how we can use the MySQL LOG10() function with proper syntax and good examples. Introduction of MySQL LOG10() function To evaluate the natural logarithmic value of...

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

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

2 minutes read.

MySQL CEILING() function

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

2 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 Invisible Index

Invisible Index MySQL index decides an option to the availability of the index for the query optimizer. This index shows the visible and invisible options to displays the index column in...

3 minutes read.

MySQL POW() function

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

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