可以使用常用的算术运算符。结果按照以下规则确定: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 withBIGINT(64-bit) precision if both operands are integers.如果两个操作数都是整数,并且其中任何一个是无符号的,则结果为无符号整数。对于减法,如果启用了
NO_UNSIGNED_SUBTRACTIONSQL 模式,即使任何操作数是无符号的,结果也为有符号整数。If both operands are integers and any of them are unsigned, the result is an unsigned integer. For subtraction, if theNO_UNSIGNED_SUBTRACTIONSQL 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 thediv_precision_incrementsystem variable (which is 4 by default). For example, the result of the expression5.05 / 0.014has 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乘法: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 ofBIGINTcalculations. (See Section 13.1, “Numeric Data Types”.)部门:Division:
mysql>
SELECT 3/5;-> 0.60除以零会产生一个
NULL结果:Division by zero produces aNULLresult:mysql>
SELECT 102/(1-1);-> NULL仅当在将其结果转换为整数的上下文中执行时,除法才会使用
BIGINT运算进行计算。A division is calculated withBIGINTarithmetic only if performed in a context where its result is converted to an integer.整数除法。舍弃除法结果中小数点右侧的任何分数部分。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 toDECIMALand divided usingDECIMALarithmetic before converting the result toBIGINT. If the result exceedsBIGINTrange, an error occurs.mysql>
SELECT 5 DIV 2, -5 DIV 2, 5 DIV -2, -5 DIV -2;-> 2, -2, -2, 2取模运算。返回
N除以M的余数。更多信息,请参见MOD()函数在 第 14.6.2 节,“数学函数” 中的描述。Modulo operation. Returns the remainder ofNdivided byM. For more information, see the description for theMOD()function in Section 14.6.2, “Mathematical Functions”.