top of page
CerebroSQL

MySQL: 

IF FUNCTION

Syntax:
IF(expr1,expr2,expr3)

If expr1 is TRUE (expr1 <> 0 and expr1 <> NULL), IF() returns expr2.
Otherwise, it returns expr3.

*Note*:

There is also an IF statement, which differs from the IF() function
described here. See [HELP IF statement].

If only one of expr2 or expr3 is explicitly NULL, the result type of
the IF() function is the type of the non-NULL expression.

The default return type of IF() (which may matter when it is stored
into a temporary table) is calculated as follows:

o If expr2 or expr3 produce a string, the result is a string.

If expr2 and expr3 are both strings, the result is case-sensitive if
either string is case sensitive.

o If expr2 or expr3 produce a floating-point value, the result is a
floating-point value.

o If expr2 or expr3 produce an integer, the result is an integer.

Example

mysql> SELECT IF(1>2,2,3);
-> 3
mysql> SELECT IF(1<2,'yes','no');
-> 'yes'
mysql> SELECT IF(STRCMP('test','test1'),'no','yes');
-> 'no'

bottom of page