IF
Descriptionβ
Returns valueTrue
when the condition is true, and returns valueFalseOrNull
otherwise. The return type is determined by the result of the valueTrue
/valueFalseOrNull
expression.
Syntaxβ
IF(<condition>, <value_true>, <value_false_or_null>)
Parametersβ
Parameter | Description |
---|---|
<condition> | The boolean condition to evaluate. |
<value_true> | The value to return if <condition> evaluates to true. |
<value_false_or_null> | The value to return if <condition> evaluates to false. |
Return Valueβ
The result of the IF expression:
- Returns
valueTrue
when the condition is true. - Returns
valueFalseOrNull
when the condition is false.
Examplesβ
SELECT user_id, IF(user_id = 1, 'true', 'false') AS test_if FROM test;
+---------+---------+
| user_id | test_if |
+---------+---------+
| 1 | true |
| 2 | false |
+---------+---------+