You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|[`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 (SELECT1AS n UNION ALLSELECT n+1FROM d WHERE n<10)
37
+
SELECT n, CASE WHEN n MOD 2 THEN "odd" ELSE "even" END FROM d;
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 (SELECT1AS n UNION ALLSELECT n+1FROM 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 (SELECTNULLAS x UNION ALLSELECT1 )
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 (SELECT1AS n UNION ALLSELECT n+1FROM 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