Control Flow Functions

TiDB supports all of the control flow functions available in MySQL 8.0.

NameDescription
CASECase operator
IF()If/else construct
IFNULL()Null if/else construct
NULLIF()Return NULL if expr1 = expr2

CASE

The CASE operator enables you to perform conditional logic and customize query results based on specified conditions.

Syntax:

  1. CASE
  2. WHEN condition1 THEN result1
  3. WHEN condition2 THEN result2
  4. ...
  5. ELSE default_result
  6. END

Example:

  1. WITH RECURSIVE d AS (SELECT 1 AS n UNION ALL SELECT n+1 FROM d WHERE n<10)
  2. SELECT n, CASE WHEN n MOD 2 THEN "odd" ELSE "even" END FROM d;
  1. +----+----------------------------------------------+
  2. | n | CASE WHEN n MOD 2 THEN "odd" ELSE "even" END |
  3. +----+----------------------------------------------+
  4. | 1 | odd |
  5. | 2 | even |
  6. | 3 | odd |
  7. | 4 | even |
  8. | 5 | odd |
  9. | 6 | even |
  10. | 7 | odd |
  11. | 8 | even |
  12. | 9 | odd |
  13. | 10 | even |
  14. +----+----------------------------------------------+
  15. 10 rows in set (0.00 sec)

IF()

The IF() function enables you to perform different actions based on whether a value or expression is true or not.

Syntax:

  1. IF(condition, value_if_true, value_if_false)

Example:

  1. WITH RECURSIVE d AS (SELECT 1 AS n UNION ALL SELECT n+1 FROM d WHERE n<10)
  2. SELECT n, IF(n MOD 2, "odd", "even") FROM d;
  1. +----+----------------------------+
  2. | n | IF(n MOD 2, "odd", "even") |
  3. +----+----------------------------+
  4. | 1 | odd |
  5. | 2 | even |
  6. | 3 | odd |
  7. | 4 | even |
  8. | 5 | odd |
  9. | 6 | even |
  10. | 7 | odd |
  11. | 8 | even |
  12. | 9 | odd |
  13. | 10 | even |
  14. +----+----------------------------+
  15. 10 rows in set (0.00 sec)

IFNULL()

The IFNULL(expr1,expr2) function is used to handle NULL values in queries. If expr1 is not NULL, it returns expr1; otherwise, it returns expr2.

Example:

  1. WITH data AS (SELECT NULL AS x UNION ALL SELECT 1 )
  2. SELECT x, IFNULL(x,'x has no value') FROM data;
  1. +------+----------------------------+
  2. | x | IFNULL(x,'x has no value') |
  3. +------+----------------------------+
  4. | NULL | x has no value |
  5. | 1 | 1 |
  6. +------+----------------------------+
  7. 2 rows in set (0.0006 sec)

NULLIF()

The NULLIF(expr1,expr2) function returns NULL if both arguments are the same or if the first argument is NULL. Otherwise, it returns the first argument.

Example:

  1. WITH RECURSIVE d AS (SELECT 1 AS n UNION ALL SELECT n+1 FROM d WHERE n<10)
  2. SELECT n, NULLIF(n+n, n+2) FROM d;
  1. +----+------------------+
  2. | n | NULLIF(n+n, n+2) |
  3. +----+------------------+
  4. | 1 | 2 |
  5. | 2 | NULL |
  6. | 3 | 6 |
  7. | 4 | 8 |
  8. | 5 | 10 |
  9. | 6 | 12 |
  10. | 7 | 14 |
  11. | 8 | 16 |
  12. | 9 | 18 |
  13. | 10 | 20 |
  14. +----+------------------+
  15. 10 rows in set (0.00 sec)

In this example, when n equals 2, both n+n and n+2 equal 4, making both arguments the same and causing the function to return NULL.