ID EN
Flow Control Functions

IF(expr1,expr2,expr3)

MySQL 8.4

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

Syntax

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

Examples

Example

There is also an IF statement, which differs from the IF() function described here. See Section 15.6.5.2, “IF Statement”.

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