arithmetic-functions.md 5.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
### 12.6.1 算术运算符

[](<>)

[](<>)

**表 12.9 算术运算符**

| 姓名 | 描述 |
| --- | --- |
| [`%`,`模组`](arithmetic-functions.html#operator_mod) | 模运算符 |
| [`*`](arithmetic-functions.html#operator_times) | 乘法运算符 |
| [`+`](arithmetic-functions.html#operator_plus) | 加法运算符 |
| [`-`](arithmetic-functions.html#operator_minus) | 减号运算符 |
| [`-`](arithmetic-functions.html#operator_unary-minus) | 更改参数的符号 |
| [`/`](arithmetic-functions.html#operator_divide) | 分区运算符 |
| [`分区`](arithmetic-functions.html#operator_div) | 整数除法 |

可以使用常用的算术运算符。结果根据以下规则确定:

-   如果是[`-`](arithmetic-functions.html#operator_minus),[`+`](arithmetic-functions.html#operator_plus), 和[`*`](arithmetic-functions.html#operator_times), 结果计算为[`大整数`](integer-types.html)如果两个操作数都是整数,则为(64 位)精度。

-   如果两个操作数都是整数并且其中任何一个都是无符号的,则结果是一个无符号整数。对于减法,如果[`NO_UNSIGNED_SUBTRACTION`](sql-mode.html#sqlmode_no_unsigned_subtraction)启用 SQL 模式,即使任何操作数未签名,结果也已签名。

-   如果 a 的任何操作数[`+`](arithmetic-functions.html#operator_plus),[`-`](arithmetic-functions.html#operator_minus),[`/`](arithmetic-functions.html#operator_divide),[`*`](arithmetic-functions.html#operator_times),[`%`](arithmetic-functions.html#operator_mod)是一个实数或字符串值,结果的精度是具有最大精度的操作数的精度。

-   在进行的除法中[`/`](arithmetic-functions.html#operator_divide),当使用两个精确值操作数时,结果的比例是第一个操作数的比例加上第一个操作数的值[`div_precision_increment`](server-system-variables.html#sysvar_div_precision_increment)系统变量(默认为 4)。例如,表达式的结果`5.05 / 0.014`小数点后六位 (`360.714286`)。

    这些规则适用于每个操作,因此嵌套计算意味着每个组件的精度。因此,`(14620 / 9432456) / (24250 / 9432456)`, 首先解决`(0.0014) / (0.0026)`, 最终结果有 8 位小数 (`0.60288653`)。

    由于这些规则及其应用方式,应注意确保计算的组件和子组件使用适当的精度级别。看[第 12.11 节,“转换函数和运算符”](cast-functions.html).

    有关在数值表达式求值中处理溢出的信息,请参阅[第 11.1.7 节,“超出范围和溢出处理”](out-of-range-and-overflow.html).

    算术运算符适用于数字。对于其他类型的值,可以使用替代操作。例如,要添加日期值,请使用[`DATE_ADD()`](date-and-time-functions.html#function_date-add);看[第 12.7 节,“日期和时间函数”](date-and-time-functions.html).

[](<>)[](<>)

-   [](<>)

    [`+`](arithmetic-functions.html#operator_plus)

    [](<>)[](<>)

    添加:

    ```
    mysql> SELECT 3+5;
            -> 8
    ```

-   [](<>)

    [`-`](arithmetic-functions.html#operator_minus)

    [](<>)[](<>)

    减法:

    ```
    mysql> SELECT 3-5;
            -> -2
    ```

-   [](<>)

    [`-`](arithmetic-functions.html#operator_unary-minus)

    [](<>)[](<>)[](<>)

    一元减号。此运算符更改操作数的符号。

    ```
    mysql> SELECT - 2;
            -> -2
    ```

    笔记

    如果此运算符与[`大整数`](integer-types.html),返回值也是[`大整数`](integer-types.html).这意味着您应该避免使用`-`在可能具有 -2 值的整数上<sup>63</sup>.

-   [](<>)

    [`*`](arithmetic-functions.html#operator_times)

    [](<>)[](<>)

    乘法:

    ```
    mysql> SELECT 3*5;
            -> 15
    mysql> SELECT 18014398509481984*18014398509481984.0;
            -> 324518553658426726783156020576256.0
    mysql> SELECT 18014398509481984*18014398509481984;
            -> out-of-range error
    ```

    最后一个表达式产生错误,因为整数乘法的结果超出了 64 位范围[`大整数`](integer-types.html)计算。(看[第 11.1 节,“数值数据类型”](numeric-types.html).)

-   [](<>)

    [`/`](arithmetic-functions.html#operator_divide)

    [](<>)[](<>)

    分配:

    ```
    mysql> SELECT 3/5;
            -> 0.60
    ```

    除以零产生一个`空值`结果:

    ```
    mysql> SELECT 102/(1-1);
            -> NULL
    ```

    除法计算如下[`大整数`](integer-types.html)仅当在结果转换为整数的上下文中执行算术运算。

-   [](<>)

    [`分区`](arithmetic-functions.html#operator_div)

    [](<>)

    整数除法。从除法结果中丢弃小数点右侧的任何小数部分。

    如果任一操作数具有非整数类型,则操作数将转换为[`十进制`](fixed-point-types.html)并使用[`十进制`](fixed-point-types.html)将结果转换为之前的算术[`大整数`](integer-types.html).如果结果超过`大整数`范围内,发生错误。

    ```
    mysql> SELECT 5 DIV 2, -5 DIV 2, 5 DIV -2, -5 DIV -2;
            -> 2, -2, -2, 2
    ```

-   [](<>)

    [`*`ñ`* % *`米`*`](arithmetic-functions.html#operator_mod),[`*`ñ`* 模组 *`米`*`](arithmetic-functions.html#operator_mod)

    [](<>)

    模运算。返回剩余的*`ñ`*除以*`米`*.有关详细信息,请参阅[`模组()`](mathematical-functions.html#function_mod)作用于[第 12.6.2 节,“数学函数”](mathematical-functions.html).