MySQL IF() Function
THE WORLD'S LARGEST WEB DEVELOPER SITE

MySQL IF() Function

❮ MySQL Functions

Example

Return "YES" if the condition is TRUE, or "NO" if the condition is FALSE:

SELECT IF(500<1000, "YES", "NO");
Try it Yourself »

Definition and Usage

The IF() function returns one value if a condition is TRUE, or another value if a condition is FALSE.

Note: The IF() function can return either a string or a numeric value.

Syntax

IF(condition, value_if_true, value_if_false)

Parameter Values

Parameter Description
condition Required. The value to test
value_if_true Optional. The value to return if condition evaluates to TRUE
value_if_false Optional. The value to return if condition evaluates to FALSE

Technical Details

Works in: MySQL 5.7, MySQL 5.6, MySQL 5.5, MySQL 5.1, MySQL 5.0, MySQL 4.1, MySQL 4.0, MySQL 3.23

More Examples

Example

Return 5 if the condition is TRUE, or 10 if the condition is FALSE:

SELECT IF(500<1000, 5, 10);
Try it Yourself »

Example

Test whether two strings are the same and return "YES" if they are, or "NO" if not:

SELECT IF(STRCMP("hello","bye") = 0, "YES", "NO");
Try it Yourself »

Example

Return "MORE" if the condition is TRUE, or "LESS" if the condition is FALSE:

SELECT OrderID, Quantity, IF(Quantity>10, "MORE", "LESS")
FROM OrderDetails;
Try it Yourself »

❮ MySQL Functions