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