Skip to content

Commit

Permalink
control-flow-functions: Update refrence manual links
Browse files Browse the repository at this point in the history
  • Loading branch information
dveeden committed Mar 26, 2024
1 parent 5e9a4e3 commit d0d91ef
Showing 1 changed file with 105 additions and 1 deletion.
106 changes: 105 additions & 1 deletion functions-and-operators/control-flow-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,115 @@ aliases: ['/docs/dev/functions-and-operators/control-flow-functions/','/docs/dev

# Control Flow Functions

TiDB supports all of the [control flow functions](https://dev.mysql.com/doc/refman/5.7/en/flow-control-functions.html) available in MySQL 5.7.
TiDB supports all of the [control flow functions](https://dev.mysql.com/doc/refman/8.0/en/flow-control-functions.html) available in MySQL 8.0.

| Name | Description |
|:--------------------------------------------------------------------------------------------------|:----------------------------------|
| [`CASE`](https://dev.mysql.com/doc/refman/8.0/en/flow-control-functions.html#operator_case) | Case operator |
| [`IF()`](https://dev.mysql.com/doc/refman/8.0/en/flow-control-functions.html#function_if) | If/else construct |
| [`IFNULL()`](https://dev.mysql.com/doc/refman/8.0/en/flow-control-functions.html#function_ifnull) | Null if/else construct |
| [`NULLIF()`](https://dev.mysql.com/doc/refman/8.0/en/flow-control-functions.html#function_nullif) | Return NULL if expr1 = expr2 |


## CASE

The `CASE` operator allows you to respond on multiple values and conditions.

```sql
WITH RECURSIVE d AS (SELECT 1 AS n UNION ALL SELECT n+1 FROM d WHERE n<10)
SELECT n, CASE WHEN n MOD 2 THEN "odd" ELSE "even" END FROM d
```

```
+----+----------------------------------------------+
| n | CASE WHEN n MOD 2 THEN "odd" ELSE "even" END |
+----+----------------------------------------------+
| 1 | odd |
| 2 | even |
| 3 | odd |
| 4 | even |
| 5 | odd |
| 6 | even |
| 7 | odd |
| 8 | even |
| 9 | odd |
| 10 | even |
+----+----------------------------------------------+
10 rows in set (0.00 sec)
```

## IF()

The `IF()` statement allows you to do something based on wheter a value or expression is true or not.

```sql
WITH RECURSIVE d AS (SELECT 1 AS n UNION ALL SELECT n+1 FROM d WHERE n<10)
SELECT n, IF(n MOD 2, "odd", "even") FROM d;
```

```
+----+----------------------------+
| n | IF(n MOD 2, "odd", "even") |
+----+----------------------------+
| 1 | odd |
| 2 | even |
| 3 | odd |
| 4 | even |
| 5 | odd |
| 6 | even |
| 7 | odd |
| 8 | even |
| 9 | odd |
| 10 | even |
+----+----------------------------+
10 rows in set (0.00 sec)
```

## IFNULL()

The `IFNULL()` functions returns the data of the expression if it is not NULL and if it is NULL it returns the data
of the second argument of the function.

```sql
WITH data AS (SELECT NULL AS x UNION ALL SELECT 1 )
SELECT x, IFNULL(x,'x has no value') FROM data;
```

```
+------+----------------------------+
| x | IFNULL(x,'x has no value') |
+------+----------------------------+
| NULL | x has no value |
| 1 | 1 |
+------+----------------------------+
2 rows in set (0.0006 sec)
```

## NULLIF()

The `NULLIF()` function returns null if both arguments are the same. Otherwise it returns the first argument.

```sql
WITH RECURSIVE d AS (SELECT 1 AS n UNION ALL SELECT n+1 FROM d WHERE n<10)
SELECT n, NULLIF(n+n, n+2) FROM d;
```

```
+----+------------------+
| n | NULLIF(n+n, n+2) |
+----+------------------+
| 1 | 2 |
| 2 | NULL |
| 3 | 6 |
| 4 | 8 |
| 5 | 10 |
| 6 | 12 |
| 7 | 14 |
| 8 | 16 |
| 9 | 18 |
| 10 | 20 |
+----+------------------+
10 rows in set (0.00 sec)
```

Here n+n and n+2 both evaluate to 4 if n is 2, which makes both arguments the same and the function return NULL.

0 comments on commit d0d91ef

Please sign in to comment.