Skip to content

Commit 5ccbf9c

Browse files
authored
translate 3 PRs for functions and operators (#17205)
1 parent c573438 commit 5ccbf9c

File tree

3 files changed

+158
-30
lines changed

3 files changed

+158
-30
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,71 @@
11
---
22
title: Cast 函数和操作符
33
aliases: ['/docs-cn/dev/functions-and-operators/cast-functions-and-operators/','/docs-cn/dev/reference/sql/functions-and-operators/cast-functions-and-operators/']
4-
summary: Cast 函数和操作符用于将某种数据类型的值转换为另一种数据类型。TiDB 支持使用 MySQL 5.7 中提供的所有 Cast 函数和操作符。包括 BINARY,将一个字符串转换成一个二进制字符串;CAST(),将一个值转换成一个确定类型;CONVERT(),将一个值转换成一个确定类型。TiDB 和 MySQL 对于 SELECT CAST(MeN AS CHAR) 的结果显示不一致,其中 MeN 是用科学计数法表示的双精度浮点数。MySQL 在 -15<=N<=14 时显示完整数值,在 N<-15 或 N>14 时显示科学计数法。而 TiDB 始终显示完整数值。例如,MySQL 对于 SELECT CAST(3.1415e15 AS CHAR) 的显示结果为 3.1415e15,而 TiDB 的显示结果为 3141500000000000
4+
summary: Cast 函数和操作符用于将某种数据类型的值转换为另一种数据类型。TiDB 支持使用 MySQL 8.0 中提供的所有 Cast 函数和操作符。
55
---
66

77
# Cast 函数和操作符
88

9-
Cast 函数和操作符用于将某种数据类型的值转换为另一种数据类型。TiDB 支持使用 MySQL 5.7 中提供的所有 [Cast 函数和操作符](https://dev.mysql.com/doc/refman/5.7/en/cast-functions.html)
9+
Cast 函数和操作符用于将某种数据类型的值转换为另一种数据类型。TiDB 支持使用 MySQL 8.0 中提供的所有 [Cast 函数和操作符](https://dev.mysql.com/doc/refman/8.0/en/cast-functions.html)
1010

1111
## Cast 函数和操作符表
1212

1313
| 函数和操作符名 | 功能描述 |
1414
| --------------- | ----------------------------------- |
15-
| [`BINARY`](https://dev.mysql.com/doc/refman/8.0/en/cast-functions.html#operator_binary) | 将一个字符串转换成一个二进制字符串 |
16-
| [`CAST()`](https://dev.mysql.com/doc/refman/8.0/en/cast-functions.html#function_cast) | 将一个值转换成一个确定类型 |
17-
| [`CONVERT()`](https://dev.mysql.com/doc/refman/8.0/en/cast-functions.html#function_convert) | 将一个值转换成一个确定类型 |
15+
| [`BINARY`](#binary) | 将一个字符串转换成一个二进制字符串 |
16+
| [`CAST()`](#cast) | 将一个值转换成一个确定类型 |
17+
| [`CONVERT()`](#convert) | 将一个值转换成一个确定类型 |
1818

1919
> **注意:**
2020
>
2121
> TiDB 和 MySQL 对于 `SELECT CAST(MeN AS CHAR)`(或者等价的 `SELECT CONVERT(MeM, CHAR)`)的结果显示不一致,其中 `MeN` 是用科学计数法表示的双精度浮点数。MySQL 在 `-15 <= N <= 14` 时显示完整数值,在 `N < -15``N > 14` 时显示科学计数法。而 TiDB 始终显示完整数值。例如,MySQL 对于 `SELECT CAST(3.1415e15 AS CHAR)` 的显示结果为 `3.1415e15`,而 TiDB 的显示结果为 `3141500000000000`
22+
23+
## BINARY
24+
25+
[`BINARY`](https://dev.mysql.com/doc/refman/8.0/en/cast-functions.html#operator_binary) 运算符从 MySQL 8.0.27 版本起已被废弃。建议在 TiDB 和 MySQL 中都改用 `CAST(... AS BINARY)`
26+
27+
## CAST
28+
29+
[`CAST()`](https://dev.mysql.com/doc/refman/8.0/en/cast-functions.html#function_cast) 函数用于将一个表达式的值转换为指定的数据类型。
30+
31+
此外,你还可以将该函数用于创建[多值索引](/sql-statements/sql-statement-create-index.md#多值索引)
32+
33+
示例:
34+
35+
```sql
36+
SELECT CAST(0x54694442 AS CHAR);
37+
```
38+
39+
```sql
40+
+--------------------------+
41+
| CAST(0x54694442 AS CHAR) |
42+
+--------------------------+
43+
| TiDB |
44+
+--------------------------+
45+
1 row in set (0.0002 sec)
46+
```
47+
48+
## CONVERT
49+
50+
[`CONVERT()`](https://dev.mysql.com/doc/refman/8.0/en/cast-functions.html#function_convert) 函数用于在[字符集](/character-set-and-collation.md)之间进行转换。
51+
52+
示例:
53+
54+
```sql
55+
SELECT CONVERT(0x616263 USING utf8mb4);
56+
```
57+
58+
```sql
59+
+---------------------------------+
60+
| CONVERT(0x616263 USING utf8mb4) |
61+
+---------------------------------+
62+
| abc |
63+
+---------------------------------+
64+
1 row in set (0.0004 sec)
65+
```
66+
67+
## MySQL 兼容性
68+
69+
- TiDB 不支持对空间类型 (`SPATIAL`) 进行转换操作。更多信息,请参考 [#6347](https://github.com/pingcap/tidb/issues/6347)
70+
- TiDB 不支持在 `CAST()` 中使用 `AT TIME ZONE`。更多信息,请参考 [#51742](https://github.com/pingcap/tidb/issues/51742)
71+
- `CAST(24 AS YEAR)` 在 TiDB 中返回的结果为两位数字,而在 MySQL 中返回的结果为四位数字。更多信息,请参考 [#29629](https://github.com/pingcap/tidb/issues/29629)
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
---
22
title: 数值函数与操作符
33
aliases: ['/docs-cn/dev/functions-and-operators/numeric-functions-and-operators/','/docs-cn/dev/reference/sql/functions-and-operators/numeric-functions-and-operators/']
4-
summary: TiDB 支持 MySQL 5.7 中的所有数值函数和操作符。包括加减乘除、整数除法、模运算、改变参数符号等算术操作符,以及返回乘方、自然对数、对数、正切值、余切值、正弦值、余弦值等数学函数
4+
summary: TiDB 支持 MySQL 8.0 中的所有数值函数和操作符。
55
---
66

77
# 数值函数与操作符
88

9-
TiDB 支持使用 MySQL 5.7 中提供的所有[数值函数与操作符](https://dev.mysql.com/doc/refman/5.7/en/numeric-functions.html)
9+
TiDB 支持使用 MySQL 8.0 中提供的所有[数值函数与操作符](https://dev.mysql.com/doc/refman/8.0/en/numeric-functions.html)
1010

1111
## 算术操作符
1212

@@ -24,33 +24,37 @@ TiDB 支持使用 MySQL 5.7 中提供的所有[数值函数与操作符](https:/
2424

2525
| 函数名 | 功能描述 |
2626
|:----------------------------------------------------------------------------------------------------------|:------------------------------------------------------------------|
27-
| [`POW()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_pow) | 返回参数的指定乘方的结果值 |
28-
| [`POWER()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_power) | 返回参数的指定乘方的结果值 |
27+
| [`ABS()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_abs) | 返回参数的绝对值 |
28+
| [`ACOS()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_acos) | 返回参数的反余弦值 |
29+
| [`ASIN()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_asin) | 返回参数的反正弦值 |
30+
| [`ATAN()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_atan) | 返回参数的反正切值 |
31+
| [`ATAN2(), ATAN()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_atan2) | 返回两个参数的反正切值 |
32+
| [`CEIL()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_ceil) | 返回不小于参数的最小整数值 |
33+
| [`CEILING()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_ceiling) | 返回不小于参数的最小整数值 |
34+
| [`CONV()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_conv) | 不同数基间转换数字,返回数字的字符串表示 |
35+
| [`COS()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_cos) | 返回参数的余弦值 |
36+
| [`COT()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_cot) | 返回参数的余切值 |
37+
| [`CRC32()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_crc32) | 计算循环冗余码校验值并返回一个 32 位无符号值 |
38+
| [`DEGREES()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_degrees) | 返回由弧度转化为度的参数 |
2939
| [`EXP()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_exp) | 返回 e(自然对数的底)的指定乘方后的值 |
30-
| [`SQRT()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_sqrt) | 返回非负数的二次方根 |
40+
| [`FLOOR()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_floor) | 返回不大于参数的最大整数值 |
3141
| [`LN()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_ln) | 返回参数的自然对数 |
3242
| [`LOG()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_log) | 返回第一个参数的自然对数 |
33-
| [`LOG2()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_log2) | 返回参数以 2 为底的对数 |
3443
| [`LOG10()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_log10) | 返回参数以 10 为底的对数 |
44+
| [`LOG2()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_log2) | 返回参数以 2 为底的对数 |
45+
| [`MOD()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_mod) | 返回余数 |
3546
| [`PI()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_pi) | 返回 pi 的值 |
36-
| [`TAN()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_tan) | 返回参数的正切值 |
37-
| [`COT()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_cot) | 返回参数的余切值 |
38-
| [`SIN()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_sin) | 返回参数的正弦值 |
39-
| [`COS()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_cos) | 返回参数的余弦值 |
40-
| [`ATAN()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_atan) | 返回参数的反正切值 |
41-
| [`ATAN2(), ATAN()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_atan2) | 返回两个参数的反正切值 |
42-
| [`ASIN()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_asin) | 返回参数的反正弦值 |
43-
| [`ACOS()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_acos) | 返回参数的反余弦值 |
47+
| [`POW()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_pow) | 返回参数的指定乘方的结果值 |
48+
| [`POWER()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_power) | 返回参数的指定乘方的结果值 |
4449
| [`RADIANS()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_radians) | 返回由度转化为弧度的参数 |
45-
| [`DEGREES()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_degrees) | 返回由弧度转化为度的参数 |
46-
| [`MOD()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_mod) | 返回余数 |
47-
| [`ABS()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_abs) | 返回参数的绝对值 |
48-
| [`CEIL()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_ceil) | 返回不小于参数的最小整数值 |
49-
| [`CEILING()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_ceiling) | 返回不小于参数的最小整数值 |
50-
| [`FLOOR()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_floor) | 返回不大于参数的最大整数值 |
51-
| [`ROUND()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_round) | 返回参数最近似的整数或指定小数位数的数值 |
5250
| [`RAND()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_rand) | 返回一个随机浮点值 |
51+
| [`ROUND()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_round) | 返回参数最近似的整数或指定小数位数的数值 |
5352
| [`SIGN()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_sign) | 返回参数的符号 |
54-
| [`CONV()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_conv) | 不同数基间转换数字,返回数字的字符串表示 |
53+
| [`SIN()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_sin) | 返回参数的正弦值 |
54+
| [`SQRT()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_sqrt) | 返回非负数的二次方根 |
55+
| [`TAN()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_tan) | 返回参数的正切值 |
5556
| [`TRUNCATE()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_truncate) | 返回被舍位至指定小数位数的数字 |
56-
| [`CRC32()`](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_crc32)           | 计算循环冗余码校验值并返回一个 32 位无符号值                     |
57+
58+
## 相关系统变量
59+
60+
通过 [`div_precision_increment`](/system-variables.md#div_precision_increment-从-v800-版本开始引入) 可以设置 `/` 运算符的精度。

functions-and-operators/string-functions.md

+76-2
Original file line numberDiff line numberDiff line change
@@ -415,11 +415,85 @@ SELECT CONCAT_WS(',', 'TiDB Server', 'TiKV', 'PD');
415415

416416
### [`ELT()`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_elt)
417417

418-
返回指定位置的字符串
418+
`ELT()` 函数返回索引号对应的元素。
419+
420+
```sql
421+
SELECT ELT(3, 'This', 'is', 'TiDB');
422+
```
423+
424+
```sql
425+
+------------------------------+
426+
| ELT(3, 'This', 'is', 'TiDB') |
427+
+------------------------------+
428+
| TiDB |
429+
+------------------------------+
430+
1 row in set (0.00 sec)
431+
```
432+
433+
在以上示例中,该函数返回第三个元素,即 `'TiDB'`
419434

420435
### [`EXPORT_SET()`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_export-set)
421436

422-
返回一个字符串,其中值位中设置的每个位,可以得到一个 on 字符串,而每个未设置的位,可以得到一个 off 字符串
437+
`EXPORT_SET()` 函数返回一个由指定数量 (`number_of_bits`) 的 `on`/`off` 值组成的字符串,各个值之间可以用 `separator` 分隔(可选)。这些值将基于输入的 `bits` 参数中的相应 bit 是否为 `1` 而确定,其中第一个值对应于 `bits` 中的最右边(即最低)的 bit
438+
439+
语法:
440+
441+
```sql
442+
EXPORT_SET(bits, on, off, [separator[, number_of_bits]])
443+
```
444+
445+
- `bits`:一个代表 bits 值的整数。
446+
- `on`:如果对应的 bit`1`,则返回该字符串。
447+
- `off`:如果对应的 bit`0`,则返回该字符串。
448+
- `separator`(可选):输出字符串中的分隔符。
449+
- `number_of_bits`(可选):要处理的位数。如果未设置,则默认使用 `64`(最大位数),这意味着 `bits` 将被视为一个无符号 64 位整数。
450+
451+
示例:
452+
453+
在以下示例中,`number_of_bits` 设置为 `5`,因此该函数返回由 `|` 分隔的 5 个值。`'101'` 里的 bit 值只有三位,所以其他位被视为未设置。因此,将 `number_of_bits` 设置为 `101` 或设置为 `00101` 的返回结果相同。
454+
455+
```sql
456+
SELECT EXPORT_SET(b'101',"ON",'off','|',5);
457+
```
458+
459+
```sql
460+
+-------------------------------------+
461+
| EXPORT_SET(b'101',"ON",'off','|',5) |
462+
+-------------------------------------+
463+
| ON|off|ON|off|off |
464+
+-------------------------------------+
465+
1 row in set (0.00 sec)
466+
```
467+
468+
在以下示例中,`bits` 设置为 `00001111``on` 设置为 `x``off` 设置为 `_`。这使函数在这些 `0` 位上返回 `____`,在这些 `1` 位上返回 `xxxx`。因此,从右到左处理 `00001111` 中的位时,该函数返回 `xxxx____`
469+
470+
```sql
471+
SELECT EXPORT_SET(b'00001111', 'x', '_', '', 8);
472+
```
473+
474+
```sql
475+
+------------------------------------------+
476+
| EXPORT_SET(b'00001111', 'x', '_', '', 8) |
477+
+------------------------------------------+
478+
| xxxx____ |
479+
+------------------------------------------+
480+
1 row in set (0.00 sec)
481+
```
482+
483+
在以下示例中,`bits` 设置为 `00001111``on` 设置为 `x``off` 设置为 `_`。这使函数在每个 `1` 位上返回 `x`,在每个 `0` 位上返回 `_`。因此,从右到左处理 `01010101` 中的位时,该函数返回 `x_x_x_x_`
484+
485+
```sql
486+
SELECT EXPORT_SET(b'01010101', 'x', '_', '', 8);
487+
```
488+
489+
```sql
490+
+------------------------------------------+
491+
| EXPORT_SET(b'01010101', 'x', '_', '', 8) |
492+
+------------------------------------------+
493+
| x_x_x_x_ |
494+
+------------------------------------------+
495+
1 row in set (0.00 sec)
496+
```
423497

424498
### [`FIELD()`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_field)
425499

0 commit comments

Comments
 (0)