8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 94%

14.6.1 算术运算符14.6.1 Arithmetic Operators

表 14.9 算术运算符Table 14.9 Arithmetic Operators

名称Name 说明Description
%, MOD 取模运算符Modulo operator
* 乘法运算符Multiplication operator
+ 加法运算符Addition operator
- 减法运算符Minus operator
- 改变参数的符号Change the sign of the argument
/ 除法运算符Division operator
DIVDIV 整数除法Integer division

可以使用常用的算术运算符。结果按照以下规则确定:The usual arithmetic operators are available. The result is determined according to the following rules:

  • 在 -、+ 和 * 的情况下,如果两个操作数都是整数,则结果以 BIGINT(64 位)精度计算。In the case of -, +, and *, the result is calculated with BIGINT (64-bit) precision if both operands are integers.

  • 如果两个操作数都是整数,并且其中任何一个是无符号的,则结果为无符号整数。对于减法,如果启用了 NO_UNSIGNED_SUBTRACTION SQL 模式,即使任何操作数是无符号的,结果也为有符号整数。If both operands are integers and any of them are unsigned, the result is an unsigned integer. For subtraction, if the NO_UNSIGNED_SUBTRACTION SQL mode is enabled, the result is signed even if any operand is unsigned.

  • 如果 +、-、/、*、% 的任意一个操作数是实数或字符串值,则结果的精度为精度最大的操作数的精度。If any of the operands of a +, -, /, *, % is a real or string value, the precision of the result is the precision of the operand with the maximum precision.

  • 在使用 / 进行除法运算时,当使用两个精确值操作数时,结果的小数位数等于第一个操作数的小数位数加上 div_precision_increment 系统变量的值(默认值为 4)。例如,表达式 5.05 / 0.014 的结果有六位小数(360.714286)。In division performed with /, the scale of the result when using two exact-value operands is the scale of the first operand plus the value of the div_precision_increment system variable (which is 4 by default). For example, the result of the expression 5.05 / 0.014 has a scale of six decimal places (360.714286).

这些规则适用于每次运算,因此嵌套计算意味着每个组成部分的精度。因此,(14620 / 9432456) / (24250 / 9432456) 首先会计算为 (0.0014) / (0.0026),最终结果有 8 位小数(0.60288653)。These rules are applied for each operation, such that nested calculations imply the precision of each component. Hence, (14620 / 9432456) / (24250 / 9432456), resolves first to (0.0014) / (0.0026), with the final result having 8 decimal places (0.60288653).

由于这些规则及其应用方式,应注意确保计算的组件和子组件使用适当的精度级别。请参见 第 14.10 节,“类型转换函数和运算符”。Because of these rules and the way they are applied, care should be taken to ensure that components and subcomponents of a calculation use the appropriate level of precision. See Section 14.10, “Cast Functions and Operators”.

有关处理数字表达式求值中的溢出的信息,请参见 第 13.1.7 节,“超出范围和溢出处理”。For information about handling of overflow in numeric expression evaluation, see Section 13.1.7, “Out-of-Range and Overflow Handling”.

算术运算符适用于数字。对于其他类型的值,可能有可用的替代操作。例如,要对日期值进行加法运算,请使用 DATE_ADD();参见 第 14.7 节,“日期和时间函数”。Arithmetic operators apply to numbers. For other types of values, alternative operations may be available. For example, to add date values, use DATE_ADD(); see Section 14.7, “Date and Time Functions”.

  • +

    加法:Addition:

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

    减法:Subtraction:

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

    一元负号。这个运算符会改变操作数的符号。Unary minus. This operator changes the sign of the operand.

    mysql> SELECT - 2;
            -> -2
    
    Note

    如果这个运算符用于 BIGINT,返回值也将是 BIGINT。这意味着你应当避免在可能具有值 −263 的整数上使用 -。If this operator is used with a BIGINT, the return value is also a BIGINT. This means that you should avoid using - on integers that may have the value of −263.

  • *

    乘法:Multiplication:

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

    最后一个表达式会产生错误,因为整数乘法的结果超出了 BIGINT 计算的 64 位范围。(参见 第 13.1 节,“数值数据类型”。)The last expression produces an error because the result of the integer multiplication exceeds the 64-bit range of BIGINT calculations. (See Section 13.1, “Numeric Data Types”.)

  • /

    部门:Division:

    mysql> SELECT 3/5;
            -> 0.60
    

    除以零会产生一个 NULL 结果:Division by zero produces a NULL result:

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

    仅当在将其结果转换为整数的上下文中执行时,除法才会使用 BIGINT 运算进行计算。A division is calculated with BIGINT arithmetic only if performed in a context where its result is converted to an integer.

  • DIVDIV

    整数除法。舍弃除法结果中小数点右侧的任何分数部分。Integer division. Discards from the division result any fractional part to the right of the decimal point.

    如果任一操作数是非整数类型,操作数将被转换为 DECIMAL,并使用 DECIMAL 算术进行除法运算,然后将结果转换为 BIGINT。如果结果超过 BIGINT 的范围,将发生错误。If either operand has a noninteger type, the operands are converted to DECIMAL and divided using DECIMAL arithmetic before converting the result to BIGINT. If the result exceeds BIGINT range, an error occurs.

    mysql> SELECT 5 DIV 2, -5 DIV 2, 5 DIV -2, -5 DIV -2;
            -> 2, -2, -2, 2
    
  • N % M, N MOD M

    取模运算。返回 N 除以 M 的余数。更多信息,请参见 MOD() 函数在 第 14.6.2 节,“数学函数” 中的描述。Modulo operation. Returns the remainder of N divided by M. For more information, see the description for the MOD() function in Section 14.6.2, “Mathematical Functions”.