MySQL Tutorial

MySQL Tutorial MySQL Features MySQL Database Introduction MySQL Environmental Setup MySQL Data Types MySQL variable MySQL Advance table Query MySQL database queries MySQL Entity-Relationship Model MySQL Table Query MySQL Operators MySQL logical conditions MySQL Queries MySQL Clauses Clustered vs Non-Clustered Index MySQL Full text index MySQL Descending Index MySQL Invisible Index MySQL Composite Index MySQL Prefix index MySQL Index MySQL Create index MySQL Drop Index MySQL Show index MySQL Unique index MySQL Table MySQL Variable MySQL View MySQL Constraints MySQL Command Line Client Basic Queries MySQL Stored Procedure MySQL IF Statement MySQL Subquery MySQL Triggers

MySQL Join

MySQL Join MySQL CROSS JOIN MySQL DELETE JOIN MySQL EQUI JOIN MySQL INNER JOIN MySQL Union MySQL NATURAL JOIN MySQL RIGHT JOIN MySQL SELF JOIN MySQL UPDATE JOIN

MySQL Function

MySQL Function MySQL AVG() Function MySQL SUM() Function MySQL String() Function MySQL Advance() Function MySQL Aggregate() Function MySQL COALESCE() Function MySQL Control Flow Function MySQL COUNT() Function MySQL Date And Time Function MySQL GREATEST() Function MySQL ISNULL() Function MySQL LEAST() Function MySQL Math() Function MySQL MAX() Function MySQL MIN() Function MySQL find_in_set() function MySQL ASIN() Function MySQL CEIL() function MySQL CEILING() function MySQL TAN() Function MySQL Truncate() Function MySQL FLOOR() function MySQL LN() function MySQL LOG2() function MySQL LOG10() function MySQL MOD() function MySQL PI() function MySQL POW() function MySQL RADIANS() function MySQL RAND() function MySQL ROUND() function MySQL Character Length Function MySQL Current Date Function MySQL Date Add Function MySQL Date Format Function MySQL Datediff Function MySQL Day Function MySQL Elt Function MySQL Export Set Function MySQL Field Function MySQL Format Function MySQL From Base64 Function MySQL Hex Function MySQL Insert Function MySQL Instr Function MySQL Length Function MySQL CONCAT() function MySQL FIND_IN_SET() function MySQL LIKE() function MySQL LOAD_FILE() function MySQL LOCATE() function MySQL LOG() function MySQL MONTHNAME() function MySQL NOW() function MySQL PERIOD_ADD() function MySQL PERIOD_DIFF() function MySQL POWER() function MySQL QUARTER() function MySQL REVERSE() function MySQL RIGHT() Function MySQL RPAD() function MySQL RTRIM() function MySQL SEC_TO_TIME() function MySQL SOUNDEX() function

Questions

Which Clause is Similar to Having Clause in MySQL

Misc

MySQL Error 1046 - No Database Selected Failed to Start MySQL Service Unit MySQL Service Unit not Found Import MySQL Connector Mudule not Found Error No Module Named MySQL Joins Available in MySQL MySQL Docs MySQL Download For Windows 7 64 Bit MySQL Error Code 1064 MySQL Export MySQL History MySQL Host MySQL Import MySQL Drop All Tables MySQL Drop MySQL Error Code 1175 MySQL Events MySQL Except MYSQL Foreign Key Constraint MySQL If Exists MySQL IndexOf MySQL List All Tables json_extract in MySQL TIMESTAMPDIFF in MySQL MySQL Syntax Checker Sudo MySQL Secure Installation

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.