IF(expr1,expr2,expr3) : IF « Control Flow Functions « MySQL Tutorial






Syntax:

IF(condition,true_value,false_value)

If expr1 is TRUE (expr1 <> 0 and expr1 <> NULL) then IF() returns expr2; otherwise it returns expr3.

expr1 is evaluated as an integer value.

mysql>
mysql>
mysql> SELECT IF(1>2,2,3);
+-------------+
| IF(1>2,2,3) |
+-------------+
|           3 |
+-------------+
1 row in set (0.00 sec)

mysql>
mysql> SELECT IF(1<2,'yes','no');
+--------------------+
| IF(1<2,'yes','no') |
+--------------------+
| yes                |
+--------------------+
1 row in set (0.00 sec)

mysql>
mysql>








18.3.IF
18.3.1.IF(expr1,expr2,expr3)
18.3.2.Using string comparison function with IF
18.3.3.Using IF with float point value
18.3.4.Use IF with data in a table
18.3.5.Using IF in where clause
18.3.6.Using IF() function to compare numbers
18.3.7.A number can also be returned from IF() function
18.3.8.Use and IF() and COUNT() to give us a total as COUNT() ignores NULL values