Watch the 7.3 Webinar On-Demand
This new release brings updates to Universal Storage, query
optimization, and usability that you won’t want to miss.
If the first argument is true, returns second argument. If the first argument is false or NULL, returns third argument.
IF (val, iftrue, iffalse)
The default return type depends on the type of arguments iftrue
and iffalse
. For example, if iftrue
or iffalse
result in an integer, the return type is an integer. If they are both strings, the return type is a string, and so on.
SELECT IF(0, 'a', 'b'), IF('1', 'a', 'b'), IF(NULL, 'a', 'b');
+-----------------+-------------------+--------------------+
| IF(0, 'a', 'b') | IF('1', 'a', 'b') | IF(NULL, 'a', 'b') |
+-----------------+-------------------+--------------------+
| b | a | b |
+-----------------+-------------------+--------------------+