Table 12.10 Mathematical Functions数学函数
ABS() | |
ACOS() | |
ASIN() | |
ATAN() | |
ATAN2() , ATAN() | |
CEIL() | |
CEILING() | |
CONV() | |
COS() | |
COT() | |
CRC32() | |
DEGREES() | |
EXP() | |
FLOOR() | |
LN() | |
LOG() | |
LOG10() | |
LOG2() | |
MOD() | |
PI() | |
POW() | |
POWER() | |
RADIANS() | |
RAND() | |
ROUND() | |
SIGN() | |
SIN() | |
SQRT() | |
TAN() | |
TRUNCATE() |
All mathematical functions return 发生错误时,所有数学函数都返回NULL
in the event of an error.NULL
。
Returns the absolute value of 返回X
, or NULL
if X
is NULL
.X
的绝对值,如果X
为NULL
,则返回NULL
。
The result type is derived from the argument type. 结果类型派生自参数类型。An implication of this is that 这意味着ABS(-9223372036854775808)
produces an error because the result cannot be stored in a signed BIGINT
value.ABS(-9223372036854775808)
产生错误,因为结果不能存储在有符号的BIGINT
值中。
mysql>SELECT ABS(2);
-> 2 mysql>SELECT ABS(-32);
-> 32
This function is safe to use with 此函数与BIGINT
values.BIGINT
值一起使用是安全的。
Returns the arc cosine of 返回X
, that is, the value whose cosine is X
. X
的弧余弦,即余弦为X
的值。Returns 如果NULL
if X
is not in the range -1
to 1
.X
不在-1
到1
的范围内,则返回NULL
。
mysql>SELECT ACOS(1);
-> 0 mysql>SELECT ACOS(1.0001);
-> NULL mysql>SELECT ACOS(0);
-> 1.5707963267949
Returns the arc sine of 返回X
, that is, the value whose sine is X
. X
的弧正弦,即正弦为X
的值。Returns 如果NULL
if X
is not in the range -1
to 1
.X
不在-1
到1
的范围内,则返回NULL
。
mysql>SELECT ASIN(0.2);
-> 0.20135792079033 mysql>SELECT ASIN('foo');
+-------------+ | ASIN('foo') | +-------------+ | 0 | +-------------+ 1 row in set, 1 warning (0.00 sec) mysql>SHOW WARNINGS;
+---------+------+-----------------------------------------+ | Level | Code | Message | +---------+------+-----------------------------------------+ | Warning | 1292 | Truncated incorrect DOUBLE value: 'foo' | +---------+------+-----------------------------------------+
Returns the arc tangent of 返回X
, that is, the value whose tangent is X
.X
的反正切值,即正切为X
的值。
mysql>SELECT ATAN(2);
-> 1.1071487177941 mysql>SELECT ATAN(-2);
-> -1.1071487177941
Returns the arc tangent of the two variables 返回两个变量X
and Y
. X
和Y
的反正切值。It is similar to calculating the arc tangent of 它类似于计算
, except that the signs of both arguments are used to determine the quadrant of the result.Y
/ X
的反正切,只是两个参数的符号都用来确定结果的象限。Y
/ X
mysql>SELECT ATAN(-2,2);
-> -0.78539816339745 mysql>SELECT ATAN2(PI(),0);
-> 1.5707963267949
Returns the smallest integer value not less than 返回不小于X
.X
的最小整数值。
mysql>SELECT CEILING(1.23);
-> 2 mysql>SELECT CEILING(-1.23);
-> -1
For exact-value numeric arguments, the return value has an exact-value numeric type. 对于精确值数字参数,返回值具有精确值数字类型。For string or floating-point arguments, the return value has a floating-point type.对于字符串或浮点参数,返回值具有浮点类型。
Converts numbers between different number bases. 在不同的基数之间转换数字。Returns a string representation of the number 返回数字N
, converted from base from_base
to base to_base
. N
的字符串表示形式,从基数from_base
转换为基数to_base
。Returns 如果任一参数为NULL
if any argument is NULL
. NULL
,则返回NULL
。The argument 参数N
is interpreted as an integer, but may be specified as an integer or a string. N
被解释为整数,但可以指定为整数或字符串。The minimum base is 最小基数为2
and the maximum base is 36
. 2
,最大基数为36
。If 如果from_base
is a negative number, N
is regarded as a signed number. from_base
是负数,则N
被视为有符号数。Otherwise, 否则,N
is treated as unsigned. N
被视为无符号的。CONV()
works with 64-bit precision.CONV()
以64位精度工作。
mysql>SELECT CONV('a',16,2);
-> '1010' mysql>SELECT CONV('6E',18,8);
-> '172' mysql>SELECT CONV(-17,10,-18);
-> '-H' mysql>SELECT CONV(10+'10'+'10'+X'0a',10,10);
-> '40'
Returns the cosine of 返回X
, where X
is given in radians.X
的余弦,其中X
以弧度表示。
mysql> SELECT COS(PI());
-> -1
Returns the cotangent of 返回X
.X
的余切。
mysql>SELECT COT(12);
-> -1.5726734063977 mysql>SELECT COT(0);
-> out-of-range error
Computes a cyclic redundancy check value and returns a 32-bit unsigned value. 计算循环冗余校验值并返回32位无符号值。The result is 如果参数为NULL
if the argument is NULL
. NULL
,则结果为NULL
。The argument is expected to be a string and (if possible) is treated as one if it is not.参数应为字符串,如果可能,则视为字符串。
mysql>SELECT CRC32('MySQL');
-> 3259397556 mysql>SELECT CRC32('mysql');
-> 2501908538
Returns the argument 返回参数X
, converted from radians to degrees.X
,从弧度转换为度。
mysql>SELECT DEGREES(PI());
-> 180 mysql>SELECT DEGREES(PI() / 2);
-> 90
Returns the value of e (the base of natural logarithms) raised to the power of 返回X
. e
(自然对数的底)进行X
次乘方的幂。The inverse of this function is 此函数的逆函数是LOG()
(using a single argument only) or LN()
.LOG()
(仅使用单个参数)或LN()
。
mysql>SELECT EXP(2);
-> 7.3890560989307 mysql>SELECT EXP(-2);
-> 0.13533528323661 mysql>SELECT EXP(0);
-> 1
Returns the largest integer value not greater than 返回不大于X
.X
的最大整数值。
mysql> SELECT FLOOR(1.23), FLOOR(-1.23);
-> 1, -2
For exact-value numeric arguments, the return value has an exact-value numeric type. 对于精确值数字参数,返回值具有精确值数字类型。For string or floating-point arguments, the return value has a floating-point type.对于字符串或浮点参数,返回值具有浮点类型。
Formats the number 将数字X
to a format like '#,###,###.##'
, rounded to D
decimal places, and returns the result as a string. X
格式化为'#,###,###.##'
这种格式,四舍五入到D
位小数,并以字符串形式返回结果。For details, see Section 12.8, “String Functions and Operators”.有关详细信息,请参阅第12.8节,“字符串函数和运算符”。
This function can be used to obtain a hexadecimal representation of a decimal number or a string; the manner in which it does so varies according to the argument's type. 此函数可用于获取十进制数或字符串的十六进制表示形式;它这样做的方式因参数的类型而异。See this function's description in Section 12.8, “String Functions and Operators”, for details.有关详细信息,请参阅第12.8节,“字符串函数和运算符”中对该函数的描述。
Returns the natural logarithm of 返回X
; that is, the base-e logarithm of X
. X
的自然对数;也就是X
的以e
为底的对数。If 如果X
is less than or equal to 0.0E0, the function returns NULL
and a warning “Invalid argument for logarithm” is reported.X
小于或等于0.0E0
,则函数返回NULL
并报告警告“对数参数无效”。
mysql>SELECT LN(2);
-> 0.69314718055995 mysql>SELECT LN(-2);
-> NULL
This function is synonymous with 此函数与LOG(
. X
)LOG(
函数同义。X
)The inverse of this function is the 此函数的逆函数是EXP()
function.EXP()
函数。
If called with one parameter, this function returns the natural logarithm of 如果使用一个参数调用,此函数将返回X
. X
的自然对数。If 如果X
is less than or equal to 0.0E0, the function returns NULL
and a warning “Invalid argument for logarithm” is reported.X
小于或等于0.0E0
,则函数返回NULL
并报告警告“对数参数无效”。
The inverse of this function (when called with a single argument) is the 此函数(使用单个参数调用时)的逆函数是EXP()
function.EXP()
函数。
mysql>SELECT LOG(2);
-> 0.69314718055995 mysql>SELECT LOG(-2);
-> NULL
If called with two parameters, this function returns the logarithm of 如果使用两个参数调用,此函数将返回X
to the base B
. X
的以B
为底数的对数。If 如果X
is less than or equal to 0, or if B
is less than or equal to 1, then NULL
is returned.X
小于或等于0,或者如果B小于或等于1,则返回NULL
。
mysql>SELECT LOG(2,65536);
-> 16 mysql>SELECT LOG(10,100);
-> 2 mysql>SELECT LOG(1,100);
-> NULL
LOG(
is equivalent to B
,X
)LOG(
.X
) / LOG(B
)
Returns the base-2 logarithm of 返回
. X
X
的以2为底数的对数。If 如果X
is less than or equal to 0.0E0, the function returns NULL
and a warning “Invalid argument for logarithm” is reported.X
小于或等于0.0E0
,则函数返回NULL
并报告警告“对数参数无效”。
mysql>SELECT LOG2(65536);
-> 16 mysql>SELECT LOG2(-100);
-> NULL
LOG2()
is useful for finding out how many bits a number requires for storage. LOG2()
对于找出一个数字需要多少位来存储非常有用。This function is equivalent to the expression 此函数等效于表达式LOG(
.X
) / LOG(2)LOG(
。X
) / LOG(2)
Returns the base-10 logarithm of 返回X
. X
的以10为底数的对数。If 如果X
is less than or equal to 0.0E0, the function returns NULL
and a warning “Invalid argument for logarithm” is reported.X
小于或等于0.0E0
,则函数返回NULL
并报告警告“对数参数无效”。
mysql>SELECT LOG10(2);
-> 0.30102999566398 mysql>SELECT LOG10(100);
-> 2 mysql>SELECT LOG10(-100);
-> NULL
Modulo operation. 求模运算符。Returns the remainder of 返回N
divided by M
.N
除以M
的余数。
mysql>SELECT MOD(234, 10);
-> 4 mysql>SELECT 253 % 7;
-> 1 mysql>SELECT MOD(29,9);
-> 2 mysql>SELECT 29 MOD 9;
-> 2
This function is safe to use with 此函数与BIGINT
values.BIGINT
值一起使用是安全的。
MOD()
also works on values that have a fractional part and returns the exact remainder after division:MOD()
也适用于具有小数部分的值,并在除法后返回精确的余数:
mysql> SELECT MOD(34.5,3);
-> 1.5
MOD(
N
,0) returns 返回NULL
.NULL
。
Returns the value of π (pi). 返回π(圆周率)的值。The default number of decimal places displayed is seven, but MySQL uses the full double-precision value internally.默认显示的小数位数是7,但是MySQL在内部使用完整的双精度值。
mysql>SELECT PI();
-> 3.141593 mysql>SELECT PI()+0.000000000000000000;
-> 3.141592653589793116
Returns the value of 返回X
raised to the power of Y
.X
的值,并将其提升为Y
的幂。
mysql>SELECT POW(2,2);
-> 4 mysql>SELECT POW(2,-2);
-> 0.25
This is a synonym for 这是POW()
.POW()
的同义词。
Returns the argument 返回参数X
, converted from degrees to radians. X
从度转换为弧度。(Note that π radians equals 180 degrees.)(注意π弧度等于180度。)
mysql> SELECT RADIANS(90);
-> 1.5707963267949
Returns a random floating-point value 返回一个随机浮点值v
in the range 0
<= v
< 1.0
. v
,范围为0
<= v
< 1.0
。To obtain a random integer 若要获取范围R
in the range i
<= R
< j
, use the expression FLOOR(
− i
+ RAND() * (j
. i
))i
<= R
< j
的随机整型数R
,请使用表达式FLOOR(
。i
+ RAND() * (j
- i
))For example, to obtain a random integer in the range the range 例如,要获得范围为7
<= R
< 12
, use the following statement:7<=R<12
的随机整型数,请使用以下语句:
SELECT FLOOR(7 + (RAND() * 5));
If an integer argument 如果指定整数参数N
is specified, it is used as the seed value:N
,则将其用作种子值:
With a constant initializer argument, the seed is initialized once when the statement is prepared, prior to execution.对于常量初始值设定项参数,在执行之前,在准备语句时对种子进行一次初始化。
With a nonconstant initializer argument (such as a column name), the seed is initialized with the value for each invocation of 使用非恒定的初始值设定项参数(例如列名),使用RAND()
.RAND()
每次调用的值初始化种子。
One implication of this behavior is that for equal argument values, 这种行为的一个含义是,对于相等的参数值,RAND(
returns the same value each time, and thus produces a repeatable sequence of column values. N
)RAND(
每次返回相同的值,从而生成一个可重复的列值序列。N
)In the following example, the sequence of values produced by 在下面的示例中,RAND(3)
is the same both places it occurs.RAND(3)
生成的值序列在两个位置都是相同的。
mysql>CREATE TABLE t (i INT);
Query OK, 0 rows affected (0.42 sec) mysql>INSERT INTO t VALUES(1),(2),(3);
Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql>SELECT i, RAND() FROM t;
+------+------------------+ | i | RAND() | +------+------------------+ | 1 | 0.61914388706828 | | 2 | 0.93845168309142 | | 3 | 0.83482678498591 | +------+------------------+ 3 rows in set (0.00 sec) mysql>SELECT i, RAND(3) FROM t;
+------+------------------+ | i | RAND(3) | +------+------------------+ | 1 | 0.90576975597606 | | 2 | 0.37307905813035 | | 3 | 0.14808605345719 | +------+------------------+ 3 rows in set (0.00 sec) mysql>SELECT i, RAND() FROM t;
+------+------------------+ | i | RAND() | +------+------------------+ | 1 | 0.35877890638893 | | 2 | 0.28941420772058 | | 3 | 0.37073435016976 | +------+------------------+ 3 rows in set (0.00 sec) mysql>SELECT i, RAND(3) FROM t;
+------+------------------+ | i | RAND(3) | +------+------------------+ | 1 | 0.90576975597606 | | 2 | 0.37307905813035 | | 3 | 0.14808605345719 | +------+------------------+ 3 rows in set (0.01 sec)
RAND()
in a WHERE
clause is evaluated for every row (when selecting from one table) or combination of rows (when selecting from a multiple-table join). WHERE
子句中的RAND()
对每一行(从一个表中选择时)或行组合(从多个表联接中选择时)求值。Thus, for optimizer purposes, 因此,出于优化器的目的,RAND()
is not a constant value and cannot be used for index optimizations. RAND()
不是一个常量值,不能用于索引优化。For more information, see Section 8.2.1.20, “Function Call Optimization”.有关更多信息,请参阅第8.2.1.20节,“函数调用优化”。
Use of a column with 在RAND()
values in an ORDER BY
or GROUP BY
clause may yield unexpected results because for either clause a RAND()
expression can be evaluated multiple times for the same row, each time returning a different result. ORDER BY
或GROUP BY
子句中使用具有RAND()
值的列可能会产生意外的结果,因为对于任一子句,RAND()
表达式可以对同一行进行多次求值,每次都返回不同的结果。If the goal is to retrieve rows in random order, you can use a statement like this:如果目标是按随机顺序检索行,则可以使用如下语句:
SELECT * FROM tbl_name
ORDER BY RAND();
To select a random sample from a set of rows, combine 要从一组行中选择随机样本,请将ORDER BY RAND()
with LIMIT
:ORDER BY RAND()
与LIMIT
结合使用:
SELECT * FROM table1, table2 WHERE a=b AND c<d ORDER BY RAND() LIMIT 1000;
RAND()
is not meant to be a perfect random generator. RAND()
并不是一个完美的随机生成器。It is a fast way to generate random numbers on demand that is portable between platforms for the same MySQL version.这是一种按需生成随机数的快速方法,可在同一MySQL版本的平台之间移植。
This function is unsafe for statement-based replication. 此函数对于基于语句的复制不安全。A warning is logged if you use this function when 如果在binlog_format
is set to STATEMENT
.binlog_format
设置为STATEMENT
时使用此函数,则会记录警告。
Rounds the argument 将参数X
to D
decimal places. X
四舍五入到D
个小数位。The rounding algorithm depends on the data type of 舍入算法取决于X
. X
的数据类型。如果未指定D
defaults to 0 if not specified. D
,则它默认为0。D
can be negative to cause D
digits left of the decimal point of the value X
to become zero. D
可以是负数,使值X
小数点左边的D
位变为零。The maximum absolute value for D
is 30; any digits in excess of 30 (or -30) are truncated.D
的最大绝对值为30;任何超过30(或-30)的数字都将被截断。
mysql>SELECT ROUND(-1.23);
-> -1 mysql>SELECT ROUND(-1.58);
-> -2 mysql>SELECT ROUND(1.58);
-> 2 mysql>SELECT ROUND(1.298, 1);
-> 1.3 mysql>SELECT ROUND(1.298, 0);
-> 1 mysql>SELECT ROUND(23.298, -1);
-> 20 mysql>SELECT ROUND(.12345678901234567890123456789012345, 35);
-> 0.123456789012345678901234567890
The return value has the same type as the first argument (assuming that it is integer, double, or decimal). 返回值的类型与第一个参数相同(假设它是整数、双精度或十进制)。This means that for an integer argument, the result is an integer (no decimal places):这意味着对于整数参数,结果是整数(无小数点):
mysql> SELECT ROUND(150.000,2), ROUND(150,2);
+------------------+--------------+
| ROUND(150.000,2) | ROUND(150,2) |
+------------------+--------------+
| 150.00 | 150 |
+------------------+--------------+
ROUND()
uses the following rules depending on the type of the first argument:ROUND()
根据第一个参数的类型使用以下规则:
For exact-value numbers, 对于精确的数值,ROUND()
uses the “round half away from zero” or “round toward nearest” rule: A value with a fractional part of .5 or greater is rounded up to the next integer if positive or down to the next integer if negative. ROUND()
使用“从零向上舍入一半”或“向最近的方向舍入”规则:小数部分为.5或更大的值如果为正,则向上舍入到下一个整数;如果为负,则向下舍入到下一个整数。(In other words, it is rounded away from zero.) (换句话说,它从零取整。)A value with a fractional part less than .5 is rounded down to the next integer if positive or up to the next integer if negative.小数部分小于.5的值如果为正,则向下舍入到下一个整数;如果为负,则向上舍入到下一个整数。
For approximate-value numbers, the result depends on the C library. 对于近似值数字,结果取决于C库。On many systems, this means that 在许多系统上,这意味着ROUND()
uses the “round to nearest even” rule: A value with a fractional part exactly halfway between two integers is rounded to the nearest even integer.ROUND()
使用“舍入到最接近的偶数”规则:小数部分正好位于两个整数中间的值被舍入到最接近的偶数整数。
The following example shows how rounding differs for exact and approximate values:以下示例显示精确值和近似值的舍入差异:
mysql> SELECT ROUND(2.5), ROUND(25E-1);
+------------+--------------+
| ROUND(2.5) | ROUND(25E-1) |
+------------+--------------+
| 3 | 2 |
+------------+--------------+
For more information, see Section 12.25, “Precision Math”.有关更多信息,请参阅第12.25节,“精确数学”。
In MySQL 8.0.21 and later, the data type returned by 在MySQL 8.0.21及更高版本中,ROUND()
(and TRUNCATE()
) is determined according to the rules listed here:ROUND()
(和TRUNCATE()
)返回的数据类型根据下面列出的规则确定:
When the first argument is of any integer type, the return type is always 当第一个参数是任何整数类型时,返回类型总是BIGINT
.BIGINT
。
When the first argument is of any floating-point type or of any non-numeric type, the return type is always 当第一个参数是任何浮点类型或任何非数字类型时,返回类型总是DOUBLE
.DOUBLE
。
When the first argument is a 当第一个参数是DECIMAL
value, the return type is also DECIMAL
.DECIMAL
值时,返回类型也是DECIMAL
。
The type attributes for the return value are also copied from the first argument, except in the case of 返回值的类型属性也从第一个参数中复制,但对于DECIMAL
, when the second argument is a constant value.DECIMAL
,第二个参数是常量时除外。
When the desired number of decimal places is less than the scale of the argument, the scale and the precision of the result are adjusted accordingly.当所需的小数位数小于参数的小数位数时,相应地调整结果的小数位数和精度。
In addition, for 此外,对于ROUND()
(but not for the TRUNCATE()
function), the precision is extended by one place to accomodate rounding that increases the number of significant digits. ROUND()
(但不适用于TRUNCATE()
函数),精度将扩展一个位置,以适应增加有效位数的舍入。If the second argument is negative, the return type is adjusted such that its scale is 0, with a corresponding precision. 如果第二个参数是负数,则会调整返回类型,使其小数位数为0,并具有相应的精度。For example, 例如,ROUND(99.999, 2)
returns 100.00
—the first argument is DECIMAL(5, 3)
, and the return type is DECIMAL(5, 2)
.ROUND(99.999,2)
返回100.00
,第一个参数是DECIMAL(5,3)
,返回类型是DECIMAL(5,2)
。
If the second argument is negative, the return type has scale 0 and a corresponding precision; 如果第二个参数为负数,则返回类型的小数位数为0,并具有相应的精度;ROUND(99.999, -1)
returns 100
, which is DECIMAL(3, 0)
.ROUND(99.999,-1)
返回100
,即DECIMAL(3,0)
。
Returns the sign of the argument as 根据-1
, 0
, or 1
, depending on whether X
is negative, zero, or positive.X
是负、零还是正,将参数的符号返回为-1
、0
或1
。
mysql>SELECT SIGN(-32);
-> -1 mysql>SELECT SIGN(0);
-> 0 mysql>SELECT SIGN(234);
-> 1
Returns the sine of 返回X
, where X
is given in radians.X
的正弦,其中X
以弧度表示。
mysql>SELECT SIN(PI());
-> 1.2246063538224e-16 mysql>SELECT ROUND(SIN(PI()));
-> 0
Returns the square root of a nonnegative number 返回非负数X
.X
的平方根。
mysql>SELECT SQRT(4);
-> 2 mysql>SELECT SQRT(20);
-> 4.4721359549996 mysql>SELECT SQRT(-16);
-> NULL
Returns the tangent of 返回X
, where X
is given in radians.X
的正切值,其中X
以弧度表示。
mysql>SELECT TAN(PI());
-> -1.2246063538224e-16 mysql>SELECT TAN(PI()+1);
-> 1.5574077246549
Returns the number 返回数字X
, truncated to D
decimal places. X
,截断为D
个小数位。If 如果D
is 0
, the result has no decimal point or fractional part. D
为0
,则结果没有小数点或小数部分。D
can be negative to cause D
digits left of the decimal point of the value X
to become zero.D
可以是负数,使值X
小数点左边的D
位变为零。
mysql>SELECT TRUNCATE(1.223,1);
-> 1.2 mysql>SELECT TRUNCATE(1.999,1);
-> 1.9 mysql>SELECT TRUNCATE(1.999,0);
-> 1 mysql>SELECT TRUNCATE(-1.999,1);
-> -1.9 mysql>SELECT TRUNCATE(122,-2);
-> 100 mysql>SELECT TRUNCATE(10.28*100,0);
-> 1028
All numbers are rounded toward zero.所有数字都向零舍入。
In MySQL 8.0.21 and later, the data type returned by 在MySQL 8.0.21及更高版本中,TRUNCATE()
follows the same rules that determine the return type of the ROUND()
function; for details, see the description for ROUND()
.TRUNCATE()
返回的数据类型遵循与ROUND()
函数返回类型相同的规则;有关详细信息,请参见ROUND()
的说明。