База данных: MySQL


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


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.

URL: https://dev.mysql.com/doc/refman/8.0/en/control-flow-functions.html


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'