Site icon TestingDocs.com

MySQL IF Function

MySQL IF Function

In this tutorial, we will learn about MySQL IF Function with some examples. The IF statement tests the expression in the first argument and returns its second argument if the expression is true or otherwise, returns the third argument.

Syntax

The general syntax of the IF function is as follows:

IF(exp1,exp2,exp3)

Example

mysql> SELECT IF(4=4,’True’,’False’);
+————————+
| IF(4=4,’True’,’False’) |
+————————+
| True |
+————————+
1 row in set (0.00 sec)

Here 4=4 is true so the second argument is returned by the function.

mysql> SELECT IF(5 > 9,’True’,’False’);
+————————–+
| IF(5 > 9,’True’,’False’) |
+————————–+
| False |
+————————–+
1 row in set (0.00 sec)

 

Since 5 is not greater than 9 the third argument False is returned. There is also IF statement that is used in stored procedures. The IF statement is a conditional statement.

 

MySQL Tutorials

MySQL Tutorials on this website:

https://www.testingdocs.com/mysql-tutorials-for-beginners/

For more information on MySQL Database:

https://www.mysql.com/

Exit mobile version