Skip to content

Commit e4992ea

Browse files
authored
add if in table basic function from 2091 (#1090)
1 parent 90e38c4 commit e4992ea

8 files changed

Lines changed: 371 additions & 1 deletion

File tree

src/UserGuide/Master/Table/SQL-Manual/Basis-Function_apache.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,50 @@ Returns the first non-null value from the given list of parameters.
12861286
coalesce(value1, value2[, ...])
12871287
```
12881288

1289+
### 7.3 IF Expression
1290+
The IF expression has two forms: one that specifies only the true value, and another that specifies both the true value and the false value.
1291+
1292+
| Form | Description | Output Type Restrictions |
1293+
| ---------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------ | ------------------------ |
1294+
| `IF(condition, true_value)` | If the condition evaluates to true, `true_value` is computed and returned; otherwise, `null` is returned and `true_value` is not evaluated. ||
1295+
| `IF(condition, true_value, false_value)` | If the condition evaluates to true, `true_value` is computed and returned; otherwise, `false_value` is computed and returned. | The data types of `true_value` and `false_value` **must be exactly the same**. Implicit type conversion is not supported. |
1296+
1297+
> Supported since V2.0.9-beta
1298+
1299+
**Examples:**
1300+
1301+
1. Equivalent examples of IF and CASE expressions:
1302+
```SQL
1303+
-- IF syntax
1304+
SELECT
1305+
device_id,
1306+
temperature,
1307+
IF(temperature > 85, 'High Value', 'Low Value')
1308+
FROM table1;
1309+
1310+
-- Equivalent CASE syntax
1311+
SELECT
1312+
device_id,
1313+
temperature,
1314+
CASE
1315+
WHEN temperature > 85 THEN 'High Value'
1316+
ELSE 'Low Value'
1317+
END
1318+
FROM table1;
1319+
```
1320+
1321+
2. Output type restriction examples:
1322+
```SQL
1323+
-- Succeeds
1324+
-- temperature (float) and humidity (float) have matching types
1325+
SELECT IF(temperature > 85, temperature, humidity) FROM table1;
1326+
1327+
-- Fails
1328+
-- temperature (float) and status (boolean) have mismatched types
1329+
SELECT IF(temperature > 85, temperature, status) FROM table1;
1330+
```
1331+
1332+
12891333
## 8. Conversion Functions
12901334

12911335
### 8.1 Conversion Functions

src/UserGuide/Master/Table/SQL-Manual/Basis-Function_timecho.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,51 @@ Returns the first non-null value from the given list of parameters.
12871287
coalesce(value1, value2[, ...])
12881288
```
12891289

1290+
### 7.3 IF Expression
1291+
1292+
The IF expression has two forms: one that specifies only the true value, and another that specifies both the true value and the false value.
1293+
1294+
| Form | Description | Output Type Restrictions |
1295+
| ---------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------ | ------------------------ |
1296+
| `IF(condition, true_value)` | If the condition evaluates to true, `true_value` is computed and returned; otherwise, `null` is returned and `true_value` is not evaluated. ||
1297+
| `IF(condition, true_value, false_value)` | If the condition evaluates to true, `true_value` is computed and returned; otherwise, `false_value` is computed and returned. | The data types of `true_value` and `false_value` **must be exactly the same**. Implicit type conversion is not supported. |
1298+
1299+
> Supported since V2.0.9.1
1300+
1301+
**Examples:**
1302+
1303+
1. Equivalent examples of IF and CASE expressions:
1304+
```SQL
1305+
-- IF syntax
1306+
SELECT
1307+
device_id,
1308+
temperature,
1309+
IF(temperature > 85, 'High Value', 'Low Value')
1310+
FROM table1;
1311+
1312+
-- Equivalent CASE syntax
1313+
SELECT
1314+
device_id,
1315+
temperature,
1316+
CASE
1317+
WHEN temperature > 85 THEN 'High Value'
1318+
ELSE 'Low Value'
1319+
END
1320+
FROM table1;
1321+
```
1322+
1323+
2. Output type restriction examples:
1324+
```SQL
1325+
-- Succeeds
1326+
-- temperature (float) and humidity (float) have matching types
1327+
SELECT IF(temperature > 85, temperature, humidity) FROM table1;
1328+
1329+
-- Fails
1330+
-- temperature (float) and status (boolean) have mismatched types
1331+
SELECT IF(temperature > 85, temperature, status) FROM table1;
1332+
```
1333+
1334+
12901335
## 8. Conversion Functions
12911336

12921337
### 8.1 Conversion Functions

src/UserGuide/latest-Table/SQL-Manual/Basis-Function_apache.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,50 @@ Returns the first non-null value from the given list of parameters.
12861286
coalesce(value1, value2[, ...])
12871287
```
12881288

1289+
### 7.3 IF Expression
1290+
The IF expression has two forms: one that specifies only the true value, and another that specifies both the true value and the false value.
1291+
1292+
| Form | Description | Output Type Restrictions |
1293+
| ---------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------ | ------------------------ |
1294+
| `IF(condition, true_value)` | If the condition evaluates to true, `true_value` is computed and returned; otherwise, `null` is returned and `true_value` is not evaluated. ||
1295+
| `IF(condition, true_value, false_value)` | If the condition evaluates to true, `true_value` is computed and returned; otherwise, `false_value` is computed and returned. | The data types of `true_value` and `false_value` **must be exactly the same**. Implicit type conversion is not supported. |
1296+
1297+
> Supported since V2.0.9-beta
1298+
1299+
**Examples:**
1300+
1301+
1. Equivalent examples of IF and CASE expressions:
1302+
```SQL
1303+
-- IF syntax
1304+
SELECT
1305+
device_id,
1306+
temperature,
1307+
IF(temperature > 85, 'High Value', 'Low Value')
1308+
FROM table1;
1309+
1310+
-- Equivalent CASE syntax
1311+
SELECT
1312+
device_id,
1313+
temperature,
1314+
CASE
1315+
WHEN temperature > 85 THEN 'High Value'
1316+
ELSE 'Low Value'
1317+
END
1318+
FROM table1;
1319+
```
1320+
1321+
2. Output type restriction examples:
1322+
```SQL
1323+
-- Succeeds
1324+
-- temperature (float) and humidity (float) have matching types
1325+
SELECT IF(temperature > 85, temperature, humidity) FROM table1;
1326+
1327+
-- Fails
1328+
-- temperature (float) and status (boolean) have mismatched types
1329+
SELECT IF(temperature > 85, temperature, status) FROM table1;
1330+
```
1331+
1332+
12891333
## 8. Conversion Functions
12901334

12911335
### 8.1 Conversion Functions

src/UserGuide/latest-Table/SQL-Manual/Basis-Function_timecho.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,51 @@ Returns the first non-null value from the given list of parameters.
12871287
coalesce(value1, value2[, ...])
12881288
```
12891289

1290+
### 7.3 IF Expression
1291+
1292+
The IF expression has two forms: one that specifies only the true value, and another that specifies both the true value and the false value.
1293+
1294+
| Form | Description | Output Type Restrictions |
1295+
| ---------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------ | ------------------------ |
1296+
| `IF(condition, true_value)` | If the condition evaluates to true, `true_value` is computed and returned; otherwise, `null` is returned and `true_value` is not evaluated. ||
1297+
| `IF(condition, true_value, false_value)` | If the condition evaluates to true, `true_value` is computed and returned; otherwise, `false_value` is computed and returned. | The data types of `true_value` and `false_value` **must be exactly the same**. Implicit type conversion is not supported. |
1298+
1299+
> Supported since V2.0.9.1
1300+
1301+
**Examples:**
1302+
1303+
1. Equivalent examples of IF and CASE expressions:
1304+
```SQL
1305+
-- IF syntax
1306+
SELECT
1307+
device_id,
1308+
temperature,
1309+
IF(temperature > 85, 'High Value', 'Low Value')
1310+
FROM table1;
1311+
1312+
-- Equivalent CASE syntax
1313+
SELECT
1314+
device_id,
1315+
temperature,
1316+
CASE
1317+
WHEN temperature > 85 THEN 'High Value'
1318+
ELSE 'Low Value'
1319+
END
1320+
FROM table1;
1321+
```
1322+
1323+
2. Output type restriction examples:
1324+
```SQL
1325+
-- Succeeds
1326+
-- temperature (float) and humidity (float) have matching types
1327+
SELECT IF(temperature > 85, temperature, humidity) FROM table1;
1328+
1329+
-- Fails
1330+
-- temperature (float) and status (boolean) have mismatched types
1331+
SELECT IF(temperature > 85, temperature, status) FROM table1;
1332+
```
1333+
1334+
12901335
## 8. Conversion Functions
12911336

12921337
### 8.1 Conversion Functions

src/zh/UserGuide/Master/Table/SQL-Manual/Basis-Function_apache.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1265,14 +1265,61 @@ SELECT a, b,
12651265
coalesce(value1, value2[, ...])
12661266
```
12671267

1268+
### 7.3 IF 表达式
1269+
1270+
IF 表达式有两种形式:一种仅指定真值(true\_value),另一种同时指定真值和假值(false\_value)。
1271+
1272+
| 形式 | 说明 | 输出类型限制 |
1273+
| ---------------------------------------------- | ----------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------- |
1274+
| `if(condition, true_value)` | 若条件(condition)为真,则计算并返回`true_value`;否则返回`null`,且`true_value`不会被计算。 | |
1275+
| `if(condition, true_value, false_value)` | 若条件(condition)为真,则计算并返回`true_value`;否则计算并返回`false_value`| `true_value``false_value`的数据类型​**必须完全一致**​,不支持隐式类型转换。 |
1276+
1277+
> V 2.0.9-beta 版本起支持
1278+
1279+
**示例:**
1280+
1281+
1. IF 表达式和 CASE 表达式等价示例:
1282+
1283+
```SQL
1284+
-- IF 写法
1285+
SELECT
1286+
device_id,
1287+
temperature,
1288+
IF(temperature > 85, 'High Value', 'Low Value')
1289+
FROM table1;
1290+
1291+
-- CASE 等价写法
1292+
SELECT
1293+
device_id,
1294+
temperature,
1295+
CASE
1296+
WHEN temperature > 85 THEN 'High Value'
1297+
ELSE 'Low Value'
1298+
END
1299+
FROM table1;
1300+
```
1301+
1302+
2. 输出类型限制示例:
1303+
1304+
```SQL
1305+
-- 成功
1306+
-- temperature(float) 和 humidity(float) 类型一致
1307+
select if(temperature > 85, temperature, humidity) from table1
1308+
1309+
-- 失败
1310+
-- temperature(float) 和 status(boolean) 类型不一致
1311+
select if(temperature > 85, temperature, status) from table1
1312+
```
1313+
1314+
12681315
## 8. 转换函数
12691316

12701317
### 8.1 转换函数
12711318

12721319
#### 8.1.1 cast(value AS type) → type
12731320

12741321
1. 显式地将一个值转换为指定类型。
1275-
2. 可以用于将字符串(varchar)转换为数值类型,或数值转换为字符串类型
1322+
2. 可以用于将字符串(varchar)转换为数值类型,或数值转换为字符串类型
12761323
3. 如果转换失败,将抛出运行时错误。
12771324

12781325
示例:

src/zh/UserGuide/Master/Table/SQL-Manual/Basis-Function_timecho.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,55 @@ SELECT a, b,
12651265
coalesce(value1, value2[, ...])
12661266
```
12671267

1268+
1269+
### 7.3 IF 表达式
1270+
1271+
IF 表达式有两种形式:一种仅指定真值(true\_value),另一种同时指定真值和假值(false\_value)。
1272+
1273+
| 形式 | 说明 | 输出类型限制 |
1274+
| ---------------------------------------------- | ----------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------- |
1275+
| `if(condition, true_value)` | 若条件(condition)为真,则计算并返回`true_value`;否则返回`null`,且`true_value`不会被计算。 | |
1276+
| `if(condition, true_value, false_value)` | 若条件(condition)为真,则计算并返回`true_value`;否则计算并返回`false_value`| `true_value``false_value`的数据类型​**必须完全一致**​,不支持隐式类型转换。 |
1277+
1278+
> V 2.0.9.1 版本起支持
1279+
1280+
**示例:**
1281+
1282+
1. IF 表达式和 CASE 表达式等价示例:
1283+
1284+
```SQL
1285+
-- IF 写法
1286+
SELECT
1287+
device_id,
1288+
temperature,
1289+
IF(temperature > 85, 'High Value', 'Low Value')
1290+
FROM table1;
1291+
1292+
-- CASE 等价写法
1293+
SELECT
1294+
device_id,
1295+
temperature,
1296+
CASE
1297+
WHEN temperature > 85 THEN 'High Value'
1298+
ELSE 'Low Value'
1299+
END
1300+
FROM table1;
1301+
```
1302+
1303+
2. 输出类型限制示例:
1304+
1305+
```SQL
1306+
-- 成功
1307+
-- temperature(float) 和 humidity(float) 类型一致
1308+
select if(temperature > 85, temperature, humidity) from table1
1309+
1310+
-- 失败
1311+
-- temperature(float) 和 status(boolean) 类型不一致
1312+
select if(temperature > 85, temperature, status) from table1
1313+
```
1314+
1315+
1316+
12681317
## 8. 转换函数
12691318

12701319
### 8.1 转换函数

src/zh/UserGuide/latest-Table/SQL-Manual/Basis-Function_apache.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,53 @@ SELECT a, b,
12651265
coalesce(value1, value2[, ...])
12661266
```
12671267

1268+
### 7.3 IF 表达式
1269+
1270+
IF 表达式有两种形式:一种仅指定真值(true\_value),另一种同时指定真值和假值(false\_value)。
1271+
1272+
| 形式 | 说明 | 输出类型限制 |
1273+
| ---------------------------------------------- | ----------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------- |
1274+
| `if(condition, true_value)` | 若条件(condition)为真,则计算并返回`true_value`;否则返回`null`,且`true_value`不会被计算。 | |
1275+
| `if(condition, true_value, false_value)` | 若条件(condition)为真,则计算并返回`true_value`;否则计算并返回`false_value`| `true_value``false_value`的数据类型​**必须完全一致**​,不支持隐式类型转换。 |
1276+
1277+
> V2.0.9-beta 版本起支持
1278+
1279+
**示例:**
1280+
1281+
1. IF 表达式和 CASE 表达式等价示例:
1282+
1283+
```SQL
1284+
-- IF 写法
1285+
SELECT
1286+
device_id,
1287+
temperature,
1288+
IF(temperature > 85, 'High Value', 'Low Value')
1289+
FROM table1;
1290+
1291+
-- CASE 等价写法
1292+
SELECT
1293+
device_id,
1294+
temperature,
1295+
CASE
1296+
WHEN temperature > 85 THEN 'High Value'
1297+
ELSE 'Low Value'
1298+
END
1299+
FROM table1;
1300+
```
1301+
1302+
2. 输出类型限制示例:
1303+
1304+
```SQL
1305+
-- 成功
1306+
-- temperature(float) 和 humidity(float) 类型一致
1307+
select if(temperature > 85, temperature, humidity) from table1
1308+
1309+
-- 失败
1310+
-- temperature(float) 和 status(boolean) 类型不一致
1311+
select if(temperature > 85, temperature, status) from table1
1312+
```
1313+
1314+
12681315
## 8. 转换函数
12691316

12701317
### 8.1 转换函数

0 commit comments

Comments
 (0)