Table 12.9 Arithmetic Operators算术运算符
The usual arithmetic operators are available. 通常的算术运算符是可用的。The result is determined according to the following rules:根据以下规则确定结果:
In the case of 在-, +, and *, the result is calculated with BIGINT (64-bit) precision if both operands are integers.-、+和*的情况下,如果两个操作数都是整数,则使用BIGINT(64位)精度计算结果。
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.NO_UNSIGNED_SUBTRACTIONSQL模式,则即使任何操作数都是无符号的,结果也是有符号的。
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.+、-、/、*、%的任何操作数是实数或字符串值,则结果的精度为具有最大精度的操作数的精度。
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). /执行除法时,使用两个精确值操作数时,结果的小数位数是第一个操作数的小数位数加上div_precision_increment系统变量的值(默认值为4)。For example, the result of the expression 例如,表达式5.05 / 0.014 has a scale of six decimal places (360.714286).5.05/0.014的结果的小数点后六位(360.714286)。
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).(14620 / 9432456) / (24250 / 9432456)首先解析为(0.0014) / (0.0026),最终结果有8位小数(0.60288653)。
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 12.11, “Cast Functions and Operators”.请参阅第12.11节,“强制转换函数和运算符”。
For information about handling of overflow in numeric expression evaluation, see Section 11.1.7, “Out-of-Range and Overflow Handling”.有关数字表达式求值中溢出处理的信息,请参阅第11.1.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 12.7, “Date and Time Functions”.DATE_ADD();请参阅第12.7节,“日期和时间函数”。
Addition:加法:
mysql> SELECT 3+5;
-> 8Subtraction:减法:
mysql> SELECT 3-5;
-> -2Unary minus. 一元减。This operator changes the sign of the operand.此运算符更改操作数的符号。
mysql> SELECT - 2;
-> -2
Multiplication:乘法:
mysql>SELECT 3*5;-> 15 mysql>SELECT 18014398509481984*18014398509481984.0;-> 324518553658426726783156020576256.0 mysql>SELECT 18014398509481984*18014398509481984;-> out-of-range error
The last expression produces an error because the result of the integer multiplication exceeds the 64-bit range of 最后一个表达式产生错误,因为整数乘法的结果超出了BIGINT calculations. BIGINT计算的64位范围。(See Section 11.1, “Numeric Data Types”.)(请参阅第11.1节,“数字数据类型”。)
Division:除法:
mysql> SELECT 3/5;
-> 0.60
Division by zero produces a 被零除产生NULL result:NULL结果:
mysql> SELECT 102/(1-1);
-> NULL
A division is calculated with 除法只有在其结果转换为整数的上下文中执行时,才使用BIGINT arithmetic only if performed in a context where its result is converted to an integer.BIGINT算术进行计算。
Integer division. 整除。Discards from the division result any fractional part to the right of the decimal point.从除法结果中丢弃小数点右侧的任何小数部分。
If either operand has a noninteger type, the operands are converted to 如果任一操作数的类型为非整数,则在将结果转换为DECIMAL and divided using DECIMAL arithmetic before converting the result to BIGINT. BIGINT之前,操作数将转换为DECIMAL并使用DECIMAL算术进行除法。If the result exceeds 如果结果超出BIGINT range, an error occurs.BIGINT范围,则会发生错误。
mysql> SELECT 5 DIV 2, -5 DIV 2, 5 DIV -2, -5 DIV -2;
-> 2, -2, -2, 2Modulo operation. 求模运算。Returns the remainder of 返回N divided by M. N被M除的余数。For more information, see the description for the 有关更多信息,请参阅第12.6.2节,“数学函数”中对MOD() function in Section 12.6.2, “Mathematical Functions”.MOD()函数的说明。