Skip to content

Commit f69f43f

Browse files
authored
control-flow-functions: Update reference manual links (#16849)
1 parent e95cc3b commit f69f43f

File tree

1 file changed

+132
-5
lines changed

1 file changed

+132
-5
lines changed

functions-and-operators/control-flow-functions.md

Lines changed: 132 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,138 @@ aliases: ['/docs/dev/functions-and-operators/control-flow-functions/','/docs/dev
66

77
# Control Flow Functions
88

9-
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.
9+
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.
1010

1111
| Name | Description |
1212
|:--------------------------------------------------------------------------------------------------|:----------------------------------|
13-
| [`CASE`](https://dev.mysql.com/doc/refman/8.0/en/flow-control-functions.html#operator_case) | Case operator |
14-
| [`IF()`](https://dev.mysql.com/doc/refman/8.0/en/flow-control-functions.html#function_if) | If/else construct |
15-
| [`IFNULL()`](https://dev.mysql.com/doc/refman/8.0/en/flow-control-functions.html#function_ifnull) | Null if/else construct |
16-
| [`NULLIF()`](https://dev.mysql.com/doc/refman/8.0/en/flow-control-functions.html#function_nullif) | Return NULL if expr1 = expr2 |
13+
| [`CASE`](#case) | Case operator |
14+
| [`IF()`](#if) | If/else construct |
15+
| [`IFNULL()`](#ifnull) | Null if/else construct |
16+
| [`NULLIF()`](#nullif) | Return `NULL` if expr1 = expr2 |
17+
18+
## CASE
19+
20+
The [`CASE`](https://dev.mysql.com/doc/refman/8.0/en/flow-control-functions.html#operator_case) operator enables you to perform conditional logic and customize query results based on specified conditions.
21+
22+
Syntax:
23+
24+
```sql
25+
CASE
26+
WHEN condition1 THEN result1
27+
WHEN condition2 THEN result2
28+
...
29+
ELSE default_result
30+
END
31+
```
32+
33+
Example:
34+
35+
```sql
36+
WITH RECURSIVE d AS (SELECT 1 AS n UNION ALL SELECT n+1 FROM d WHERE n<10)
37+
SELECT n, CASE WHEN n MOD 2 THEN "odd" ELSE "even" END FROM d;
38+
```
39+
40+
```
41+
+----+----------------------------------------------+
42+
| n | CASE WHEN n MOD 2 THEN "odd" ELSE "even" END |
43+
+----+----------------------------------------------+
44+
| 1 | odd |
45+
| 2 | even |
46+
| 3 | odd |
47+
| 4 | even |
48+
| 5 | odd |
49+
| 6 | even |
50+
| 7 | odd |
51+
| 8 | even |
52+
| 9 | odd |
53+
| 10 | even |
54+
+----+----------------------------------------------+
55+
10 rows in set (0.00 sec)
56+
```
57+
58+
## IF()
59+
60+
The [`IF()`](https://dev.mysql.com/doc/refman/8.0/en/flow-control-functions.html#function_if) function enables you to perform different actions based on whether a value or expression is true or not.
61+
62+
Syntax:
63+
64+
```sql
65+
IF(condition, value_if_true, value_if_false)
66+
```
67+
68+
Example:
69+
70+
```sql
71+
WITH RECURSIVE d AS (SELECT 1 AS n UNION ALL SELECT n+1 FROM d WHERE n<10)
72+
SELECT n, IF(n MOD 2, "odd", "even") FROM d;
73+
```
74+
75+
```
76+
+----+----------------------------+
77+
| n | IF(n MOD 2, "odd", "even") |
78+
+----+----------------------------+
79+
| 1 | odd |
80+
| 2 | even |
81+
| 3 | odd |
82+
| 4 | even |
83+
| 5 | odd |
84+
| 6 | even |
85+
| 7 | odd |
86+
| 8 | even |
87+
| 9 | odd |
88+
| 10 | even |
89+
+----+----------------------------+
90+
10 rows in set (0.00 sec)
91+
```
92+
93+
## IFNULL()
94+
95+
The [`IFNULL(expr1,expr2)`](https://dev.mysql.com/doc/refman/8.0/en/flow-control-functions.html#function_ifnull) function is used to handle NULL values in queries. If `expr1` is not `NULL`, it returns `expr1`; otherwise, it returns `expr2`.
96+
97+
Example:
98+
99+
```sql
100+
WITH data AS (SELECT NULL AS x UNION ALL SELECT 1 )
101+
SELECT x, IFNULL(x,'x has no value') FROM data;
102+
```
103+
104+
```
105+
+------+----------------------------+
106+
| x | IFNULL(x,'x has no value') |
107+
+------+----------------------------+
108+
| NULL | x has no value |
109+
| 1 | 1 |
110+
+------+----------------------------+
111+
2 rows in set (0.0006 sec)
112+
```
113+
114+
## NULLIF()
115+
116+
The [`NULLIF(expr1,expr2)`](https://dev.mysql.com/doc/refman/8.0/en/flow-control-functions.html#function_nullif) function returns `NULL` if both arguments are the same or if the first argument is `NULL`. Otherwise, it returns the first argument.
117+
118+
Example:
119+
120+
```sql
121+
WITH RECURSIVE d AS (SELECT 1 AS n UNION ALL SELECT n+1 FROM d WHERE n<10)
122+
SELECT n, NULLIF(n+n, n+2) FROM d;
123+
```
124+
125+
```
126+
+----+------------------+
127+
| n | NULLIF(n+n, n+2) |
128+
+----+------------------+
129+
| 1 | 2 |
130+
| 2 | NULL |
131+
| 3 | 6 |
132+
| 4 | 8 |
133+
| 5 | 10 |
134+
| 6 | 12 |
135+
| 7 | 14 |
136+
| 8 | 16 |
137+
| 9 | 18 |
138+
| 10 | 20 |
139+
+----+------------------+
140+
10 rows in set (0.00 sec)
141+
```
142+
143+
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`.

0 commit comments

Comments
 (0)