表 14.10 数学函数Table 14.10 Mathematical Functions
| 名称Name | 说明Description |
|---|---|
ABS() |
返回绝对值Return the absolute value |
ACOS() |
返回反余弦Return the arc cosine |
ASIN() |
返回反正弦Return the arc sine |
ATAN() |
返回反正切Return the arc tangent |
ATAN2(), ATAN() |
返回两个参数的反正切Return the arc tangent of the two arguments |
CEIL() |
返回不小于参数的最小整数值Return the smallest integer value not less than the argument |
CEILING() |
返回不小于参数的最小整数值Return the smallest integer value not less than the argument |
CONV() |
在不同的数字进制之间转换数字Convert numbers between different number bases |
COS() |
返回余弦值Return the cosine |
COT() |
返回余切Return the cotangent |
CRC32() |
计算循环冗余校验值Compute a cyclic redundancy check value |
DEGREES() |
将弧度转换为度Convert radians to degrees |
EXP() |
乘方Raise to the power of |
FLOOR() |
返回不大于参数的最大整数值Return the largest integer value not greater than the argument |
LN() |
返回参数的自然对数Return the natural logarithm of the argument |
LOG() |
返回第一个参数的自然对数Return the natural logarithm of the first argument |
LOG10() |
返回参数的以 10 为底的对数Return the base-10 logarithm of the argument |
LOG2() |
返回参数的以 2 为底的对数Return the base-2 logarithm of the argument |
MOD() |
返回余数Return the remainder |
PI() |
返回圆周率的值Return the value of pi |
POW() |
返回指定次方的参数Return the argument raised to the specified power |
POWER() |
返回指定次方的参数Return the argument raised to the specified power |
RADIANS() |
返回转换为弧度的参数Return argument converted to radians |
RAND() |
返回一个随机浮点值Return a random floating-point value |
ROUND() |
围绕论点Round the argument |
SIGN() |
返回参数的符号Return the sign of the argument |
SIN() |
返回参数的正弦值Return the sine of the argument |
SQRT() |
返回参数的平方根Return the square root of the argument |
TAN() |
返回参数的正切值Return the tangent of the argument |
TRUNCATE() |
截断到指定的小数位数Truncate to specified number of decimal places |
所有数学函数在发生错误时都会返回 NULL。All mathematical functions return NULL in the
event of an error.
返回
X的绝对值,如果X为NULL,则返回NULL。Returns the absolute value ofX, orNULLifXisNULL.结果类型由参数类型决定。这意味着
ABS(-9223372036854775808)会产生错误,因为结果无法存储在有符号BIGINT值中。The result type is derived from the argument type. An implication of this is thatABS(-9223372036854775808)produces an error because the result cannot be stored in a signedBIGINTvalue.mysql>
SELECT ABS(2);-> 2 mysql>SELECT ABS(-32);-> 32此函数可安全用于
BIGINT值。This function is safe to use withBIGINTvalues.返回
X的反余弦,即余弦值为X的值。如果X不在-1到1范围内,或者X为NULL,则返回NULL。Returns the arc cosine ofX, that is, the value whose cosine isX. ReturnsNULLifXis not in the range-1to1, or ifXisNULL.mysql>
SELECT ACOS(1);-> 0 mysql>SELECT ACOS(1.0001);-> NULL mysql>SELECT ACOS(0);-> 1.5707963267949返回
X的反正弦,即正弦值为X的值。如果X不在-1到1的范围内,或者X为NULL,则返回NULL。Returns the arc sine ofX, that is, the value whose sine isX. ReturnsNULLifXis not in the range-1to1, or ifXisNULL.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' | +---------+------+-----------------------------------------+返回
X的反正切,即正切值为X的值。如果X为NULL,则返回NULLReturns the arc tangent ofX, that is, the value whose tangent isX. ReturnsNULLifXisNULLmysql>
SELECT ATAN(2);-> 1.1071487177941 mysql>SELECT ATAN(-2);-> -1.1071487177941返回两个变量
X和Y的反正切。它类似于计算Y / X的反正切,不同之处在于两个参数的符号用于确定结果的象限。如果X或Y为NULL,则返回NULL。Returns the arc tangent of the two variablesXandY. 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. ReturnsY/XNULLifXorYisNULL.mysql>
SELECT ATAN(-2,2);-> -0.78539816339745 mysql>SELECT ATAN2(PI(),0);-> 1.5707963267949返回不小于
X的最小整数值。如果X为NULL,则返回NULL。Returns the smallest integer value not less thanX. ReturnsNULLifXisNULL.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.
在不同的数字进制之间转换数字。返回数字
N的字符串表示形式,该数字已从进制from_base转换为进制to_base。如果任何参数为NULL,则返回NULL。参数N被视为整数,但可以以整数或字符串的形式指定。最小进制为2,最大进制为36。如果from_base是负数,则N被视为有符号数。否则,N被视为无符号数。CONV()支持 64 位精度。Converts numbers between different number bases. Returns a string representation of the numberN, converted from basefrom_baseto baseto_base. ReturnsNULLif any argument isNULL. The argumentNis interpreted as an integer, but may be specified as an integer or a string. The minimum base is2and the maximum base is36. Iffrom_baseis a negative number,Nis regarded as a signed number. Otherwise,Nis treated as unsigned.CONV()works with 64-bit precision.CONV()如果其任何参数为NULL,则返回NULL。CONV()returnsNULLif any of its arguments areNULL.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'返回
X的余弦值,其中X以弧度为单位。 如果X为NULL,则返回NULL。Returns the cosine ofX, whereXis given in radians. ReturnsNULLifXisNULL.mysql>
SELECT COS(PI());-> -1返回
X的余切。 如果X为NULL,则返回NULL。Returns the cotangent ofX. ReturnsNULLifXisNULL.mysql>
SELECT COT(12);-> -1.5726734063977 mysql>SELECT COT(0);-> out-of-range error计算循环冗余校验值并返回一个 32 位无符号值。如果参数为
NULL,则结果为NULL。参数预期为字符串,如果可能的话,如果它不是字符串,也会被当作字符串处理。Computes a cyclic redundancy check value and returns a 32-bit unsigned value. The result isNULLif the argument isNULL. 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返回参数
X,将其从弧度转换为度。如果X为NULL,则返回NULL。Returns the argumentX, converted from radians to degrees. ReturnsNULLifXisNULL.mysql>
SELECT DEGREES(PI());-> 180 mysql>SELECT DEGREES(PI() / 2);-> 90返回 e(自然对数的底数)
X次幂的值。此函数的逆函数是LOG()(仅使用单个参数)或LN()。Returns the value of e (the base of natural logarithms) raised to the power ofX. The inverse of this function isLOG()(using a single argument only) orLN().如果
X为NULL,此函数返回NULL。IfXisNULL, this function returnsNULL.mysql>
SELECT EXP(2);-> 7.3890560989307 mysql>SELECT EXP(-2);-> 0.13533528323661 mysql>SELECT EXP(0);-> 1返回不大于
X的最大整数值。如果X为NULL,则返回NULL。Returns the largest integer value not greater thanX. ReturnsNULLifXisNULL.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.
将数字
X格式化为类似'#,###,###.##'的格式,四舍五入到D位小数,并将结果作为字符串返回。详情请参见第 14.8 节,“字符串函数和操作符”。Formats the numberXto a format like'#,###,###.##', rounded toDdecimal places, and returns the result as a string. For details, see Section 14.8, “String Functions and Operators”.此函数可用于获取十进制数字或字符串的十六进制表示;其实现方式取决于参数的类型。有关详细信息,请参见本函数在第 14.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 14.8, “String Functions and Operators”, for details.
返回的自然对数
X;即以e为底的对数X。如果X小于或等于 0.0E0,函数将返回NULL并生成警告“对数的参数无效”。如果NULL返回XisNULL.Returns the natural logarithm ofX; that is, the base-e logarithm ofX. IfXis less than or equal to 0.0E0, the function returnsNULLand a warning “Invalid argument for logarithm” is reported. ReturnsNULLifXisNULL.mysql>
SELECT LN(2);-> 0.69314718055995 mysql>SELECT LN(-2);-> NULL此函数与
LOG(同义。此函数的反函数是X)EXP()函数。This function is synonymous withLOG(. The inverse of this function is theX)EXP()function.如果只调用一个参数,该函数返回
X的自然对数。如果X小于或等于 0.0E0,函数返回NULL并报告警告 “对数的参数无效”。如果X或B为NULL,则返回NULL。If called with one parameter, this function returns the natural logarithm ofX. IfXis less than or equal to 0.0E0, the function returnsNULLand a warning “Invalid argument for logarithm” is reported. ReturnsNULLifXorBisNULL.这个函数的逆函数(当只传入一个参数时)是
EXP()函数。The inverse of this function (when called with a single argument) is theEXP()function.mysql>
SELECT LOG(2);-> 0.69314718055995 mysql>SELECT LOG(-2);-> NULL如果传入两个参数,该函数返回
X以B为底的对数。如果X小于或等于 0,或者B小于或等于 1,则返回NULL。If called with two parameters, this function returns the logarithm ofXto the baseB. IfXis less than or equal to 0, or ifBis less than or equal to 1, thenNULLis returned.mysql>
SELECT LOG(2,65536);-> 16 mysql>SELECT LOG(10,100);-> 2 mysql>SELECT LOG(1,100);-> NULLLOG(等价于B,X)LOG(X) / LOG(B)。LOG(is equivalent toB,X)LOG(.X) / LOG(B)返回
的以 2 为底的对数。如果XX小于或等于 0.0E0,则该函数返回NULL,并报告警告“对数的参数无效”。如果X为NULL,则返回NULL。Returns the base-2 logarithm of. IfXXis less than or equal to 0.0E0, the function returnsNULLand a warning “Invalid argument for logarithm” is reported. ReturnsNULLifXisNULL.mysql>
SELECT LOG2(65536);-> 16 mysql>SELECT LOG2(-100);-> NULLLOG2()对于确定一个数字存储所需的位数非常有用。该函数大致等同于表达式LOG(X) / LOG(2)。LOG2()is useful for finding out how many bits a number requires for storage. This function is approximately equivalent to the expressionLOG(.X) / LOG(2)返回
X的以 10 为底的对数。如果X小于或等于 0.0E0,该函数返回NULL并报告警告 “对数的参数无效”。如果X为NULL,则返回NULL。Returns the base-10 logarithm ofX. IfXis less than or equal to 0.0E0, the function returnsNULLand a warning “Invalid argument for logarithm” is reported. ReturnsNULLifXisNULL.mysql>
SELECT LOG10(2);-> 0.30102999566398 mysql>SELECT LOG10(100);-> 2 mysql>SELECT LOG10(-100);-> NULLLOG10(大致相当于X)LOG(10,。X)LOG10(is approximately equivalent toX)LOG(10,.X)取模运算。返回
N除以M的余数。如果M或N为NULL,则返回NULL。Modulo operation. Returns the remainder ofNdivided byM. ReturnsNULLifMorNisNULL.mysql>
SELECT MOD(234, 10);-> 4 mysql>SELECT 253 % 7;-> 1 mysql>SELECT MOD(29,9);-> 2 mysql>SELECT 29 MOD 9;-> 2此函数可安全用于
BIGINT值。This function is safe to use withBIGINTvalues.MOD()也适用于有小数部分的值,并返回除法后的精确余数:MOD()also works on values that have a fractional part and returns the exact remainder after division:mysql>
SELECT MOD(34.5,3);-> 1.5返回π(圆周率)的值。默认显示的小数位数为七位,但 MySQL 在内部使用全双精度值。Returns the value of π (pi). The default number of decimal places displayed is seven, but MySQL uses the full double-precision value internally.
由于此函数的返回值是双精度值,其精确表示可能因平台或实现而异。这同样适用于使用
PI()的任何表达式。请参阅第 13.1.4 节,“浮点类型(近似值)- FLOAT, DOUBLE”。Because the return value of this function is a double-precision value, its exact representation may vary between platforms or implementations. This also applies to any expressions making use ofPI(). See Section 13.1.4, “Floating-Point Types (Approximate Value) - FLOAT, DOUBLE”.mysql>
SELECT PI();-> 3.141593 mysql>SELECT PI()+0.000000000000000000;-> 3.141592653589793000返回
X的Y次幂的值。如果X或Y为NULL,则返回NULL。Returns the value ofXraised to the power ofY. ReturnsNULLifXorYisNULL.mysql>
SELECT POW(2,2);-> 4 mysql>SELECT POW(2,-2);-> 0.25返回参数
X,将其从度转换为弧度。(注意 π 弧度等于 180 度。)如果X为NULL,则返回NULL。Returns the argumentX, converted from degrees to radians. (Note that π radians equals 180 degrees.) ReturnsNULLifXisNULL.mysql>
SELECT RADIANS(90);-> 1.5707963267949返回一个随机浮点数
v,范围为0<=v<1.0。要获得范围为i<=R<j的随机整数R,使用表达式FLOOR(i + RAND() * (j−。例如,要获得范围为i))7<=R<12的随机整数,请使用以下语句:Returns a random floating-point valuevin the range0<=v<1.0. To obtain a random integerRin the rangei<=R<j, use the expressionFLOOR(−i+ RAND() * (j. For example, to obtain a random integer in the range the rangei))7<=R<12, use the following statement:SELECT FLOOR(7 + (RAND() * 5));
如果指定了一个整数参数
N,它将作为种子值使用:If an integer argumentNis specified, it is used as the seed value:使用常量初始化参数时,种子在语句准备好时就会初始化一次,而不是在执行时。With a constant initializer argument, the seed is initialized once when the statement is prepared, prior to execution.
对于非常量初始化参数(例如列名),种子在每次调用
RAND()时都会使用该值进行初始化。With a nonconstant initializer argument (such as a column name), the seed is initialized with the value for each invocation ofRAND().
这种行为的一个含义是,对于相等的参数值,
RAND(每次都会返回相同的值,从而生成可重复的列值序列。在以下示例中,N)RAND(3)产生的值序列在它出现的两个地方都是相同的。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. In the following example, the sequence of values produced byN)RAND(3)is the same both places it occurs.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()在WHERE子句中会对每一行(当从一个表中选择时)或每一行组合(当从多个表联接中选择时)进行计算。因此,对于优化器而言,RAND()不是一个常量值,不能用于索引优化。更多信息,请参见 第 10.2.1.20 节,“函数调用优化”。RAND()in aWHEREclause is evaluated for every row (when selecting from one table) or combination of rows (when selecting from a multiple-table join). Thus, for optimizer purposes,RAND()is not a constant value and cannot be used for index optimizations. For more information, see Section 10.2.1.20, “Function Call Optimization”.在
ORDER BY或RAND()子句中使用带有RAND()值的列可能会产生意外结果,因为对于任一子句,GROUP BY表达式可能会对同一行多次计算,每次返回不同的结果。如果目标是以随机顺序检索行,可以使用类似以下的语句:Use of a column withRAND()values in anORDER BYorGROUP BYclause may yield unexpected results because for either clause aRAND()expression can be evaluated multiple times for the same row, each time returning a different result. If the goal is to retrieve rows in random order, you can use a statement like this:SELECT * FROM
tbl_nameORDER BY RAND();要从一组行中选择随机样本,可以将
ORDER BY RAND()与LIMIT结合使用:To select a random sample from a set of rows, combineORDER BY RAND()withLIMIT:SELECT * FROM table1, table2 WHERE a=b AND c<d ORDER BY RAND() LIMIT 1000;
RAND()不旨在成为完美的随机生成器。它是一种快速生成随需随机数的方法,对于相同 MySQL 版本来说在各个平台之间是可移植的。RAND()is not meant to be a perfect random generator. It is a fast way to generate random numbers on demand that is portable between platforms for the same MySQL version.该函数在基于语句的复制中不安全。如果在
binlog_format设置为STATEMENT时使用此函数,将记录一个警告。This function is unsafe for statement-based replication. A warning is logged if you use this function whenbinlog_formatis set toSTATEMENT.将参数
X四舍五入到D位小数。四舍五入算法取决于X的数据类型。如果未指定,D默认为 0。D可以为负数,使X值的小数点左侧的D位变为零。D的最大绝对值为 30;超过 30(或 -30)的位数将被截断。如果X或D为NULL,函数将返回NULL。Rounds the argumentXtoDdecimal places. The rounding algorithm depends on the data type ofX.Ddefaults to 0 if not specified.Dcan be negative to causeDdigits left of the decimal point of the valueXto become zero. The maximum absolute value forDis 30; any digits in excess of 30 (or -30) are truncated. IfXorDisNULL, the function returnsNULL.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()根据第一个参数的类型使用以下规则:ROUND()uses the following rules depending on the type of the first argument:对于精确值数字,
ROUND()使用“四舍五入”或“四舍到最近整数”规则:带有 .5 或更大小数部分的值,如果为正数则向上舍入到下一个整数,如果为负数则向下舍入到下一个整数。(换句话说,它是远离零舍入的。)带有小于 .5 小数部分的值,如果为正数则向下舍入到下一个整数,如果为负数则向上舍入到下一个整数。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. (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.对于近似值数字,结果取决于 C 库。在许多系统上,这意味着
ROUND()使用“四舍五入到最近的偶数”规则:带有正好位于两个整数之间一半的小数部分的值将被四舍五入到最近的偶数整数。For approximate-value numbers, the result depends on the C library. On many systems, this means thatROUND()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.
以下示例显示了精确值和近似值的舍入方式有何不同: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 | +------------+--------------+有关更多信息,请参见 第 14.24 节,“精确数学”。For more information, see Section 14.24, “Precision Math”.
ROUND()(以及TRUNCATE())返回的数据类型根据此处列出的规则确定:The data type returned byROUND()(andTRUNCATE()) is determined according to the rules listed here:当第一个参数为任何整数类型时,返回类型始终为
BIGINT。When the first argument is of any integer type, the return type is alwaysBIGINT.当第一个参数为任何浮点类型或任何非数值类型时,返回类型始终为
DOUBLE。When the first argument is of any floating-point type or of any non-numeric type, the return type is alwaysDOUBLE.当第一个参数是
DECIMAL值时,返回类型也为DECIMAL。When the first argument is aDECIMALvalue, the return type is alsoDECIMAL.返回值的类型属性也会从第一个参数复制,除非在第二个参数为常量值时的
DECIMAL情况。The type attributes for the return value are also copied from the first argument, except in the case ofDECIMAL, when the second argument is a constant value.当所需的小数位数小于参数的刻度时,结果的刻度和精度将相应调整。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.
此外,对于
ROUND()(但不适用于TRUNCATE()函数),精度会扩展一位,以适应四舍五入导致有效数字增加的情况。如果第二个参数为负数,则返回类型会调整为小数位数为 0,并具有相应的精度。例如,ROUND(99.999, 2)返回100.00——第一个参数是DECIMAL(5, 3),返回类型是DECIMAL(5, 2)。In addition, forROUND()(but not for theTRUNCATE()function), the precision is extended by one place to accommodate rounding that increases the number of significant digits. If the second argument is negative, the return type is adjusted such that its scale is 0, with a corresponding precision. For example,ROUND(99.999, 2)returns100.00—the first argument isDECIMAL(5, 3), and the return type isDECIMAL(5, 2).如果第二个参数为负数,返回类型的小数位数为 0,并具有相应的精度;
ROUND(99.999, -1)返回100,其类型为DECIMAL(3, 0)。If the second argument is negative, the return type has scale 0 and a corresponding precision;ROUND(99.999, -1)returns100, which isDECIMAL(3, 0).
返回参数符号为,
-1,0,或1取决于X是负、零还是正。返回NULL返回XisNULL.Returns the sign of the argument as-1,0, or1, depending on whetherXis negative, zero, or positive. ReturnsNULLifXisNULL.mysql>
SELECT SIGN(-32);-> -1 mysql>SELECT SIGN(0);-> 0 mysql>SELECT SIGN(234);-> 1返回的正弦
X,其中X以弧度表示。返回NULL返回XisNULL.Returns the sine ofX, whereXis given in radians. ReturnsNULLifXisNULL.mysql>
SELECT SIN(PI());-> 1.2246063538224e-16 mysql>SELECT ROUND(SIN(PI()));-> 0返回非负数
X的平方根。如果X为NULL,该函数返回NULL。Returns the square root of a nonnegative numberX. IfXisNULL, the function returnsNULL.mysql>
SELECT SQRT(4);-> 2 mysql>SELECT SQRT(20);-> 4.4721359549996 mysql>SELECT SQRT(-16);-> NULL返回
X的正切,其中X以弧度为单位。如果X为NULL,则返回NULL。Returns the tangent ofX, whereXis given in radians. ReturnsNULLifXisNULL.mysql>
SELECT TAN(PI());-> -1.2246063538224e-16 mysql>SELECT TAN(PI()+1);-> 1.5574077246549返回数字
X,截断到D位小数。如果D为0,结果没有小数点或小数部分。D可以为负,使得X值的小数点左侧D位变为零。如果X或D为NULL,函数返回NULL。Returns the numberX, truncated toDdecimal places. IfDis0, the result has no decimal point or fractional part.Dcan be negative to causeDdigits left of the decimal point of the valueXto become zero. IfXorDisNULL, the function returnsNULL.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.
TRUNCATE()返回的数据类型遵循与ROUND()函数返回类型相同的规则;详情请参见ROUND()的描述。The data type returned byTRUNCATE()follows the same rules that determine the return type of theROUND()function; for details, see the description forROUND().