`

MySQL Numeric Functions

阅读更多
Name	Description
ABS()	Returns the absolute value of numeric expression.
ACOS()	Returns the arccosine of numeric expression. Returns NULL if the value is not in the range -1 to 1.
ASIN()	Returns the arcsine of numeric expression. Returns NULL if value is not in the range -1 to 1
ATAN()	Returns the arctangent of numeric expression.
ATAN2()	Returns the arctangent of the two variables passed to it.
BIT_AND()	Returns the bitwise AND all the bits in expression.
BIT_COUNT()	Returns the string representation of the binary value passed to it.
BIT_OR()	Returns the bitwise OR of all the bits in the passed expression.
CEIL()	Returns the smallest integer value that is not less than passed numeric expression
CEILING()	Returns the smallest integer value that is not less than passed numeric expression
CONV()	Convert numeric expression from one base to another.
COS()	Returns the cosine of passed numeric expression. The numeric expression should be expressed in radians.
COT()	Returns the cotangent of passed numeric expression.
DEGREES()	Returns numeric expression converted from radians to degrees.
EXP()	Returns the base of the natural logarithm (e) raised to the power of passed numeric expression.
FLOOR()	Returns the largest integer value that is not greater than passed numeric expression.
FORMAT()	Returns a numeric expression rounded to a number of decimal places.
GREATEST()	Returns the largest value of the input expressions.
INTERVAL()	Takes multiple expressions exp1, exp2 and exp3 so on.. and returns 0 if exp1 is less than exp2, returns 1 if exp1 is less than exp3 and so on.
LEAST()	Returns the minimum-valued input when given two or more.
LOG()	Returns the natural logarithm of the passed numeric expression.
LOG10()	Returns the base-10 logarithm of the passed numeric expression.
MOD()	Returns the remainder of one expression by diving by another expression.
OCT()	Returns the string representation of the octal value of the passed numeric expression. Returns NULL if passed value is NULL.
PI()	Returns the value of pi
POW()	Returns the value of one expression raised to the power of another expression
POWER()	Returns the value of one expression raised to the power of another expression
RADIANS()	Returns the value of passed expression converted from degrees to radians.
ROUND()	Returns numeric expression rounded to an integer. Can be used to round an expression to a number of decimal points
SIN()	Returns the sine of numeric expression given in radians.
SQRT()	Returns the non-negative square root of numeric expression.
STD()	Returns the standard deviation of the numeric expression.
STDDEV()	Returns the standard deviation of the numeric expression.
TAN()	Returns the tangent of numeric expression expressed in radians.
TRUNCATE()	Returns numeric exp1 truncated to exp2 decimal places. If exp2 is 0, then the result will have no decimal point.

ABS(X)

The ABS() function returns the absolute value of X. Consider the following example:

mysql> SELECT ABS(2);
+---------------------------------------------------------+
| ABS(2)                                                  |
+---------------------------------------------------------+
| 2                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT ABS(-2);
+---------------------------------------------------------+
| ABS(2)                                                  |
+---------------------------------------------------------+
| 2                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)
ACOS(X)

This function returns the arccosine of X. The value of X must range between .1 and 1 or NULL will be returned. Consider the following example:

mysql> SELECT ACOS(1);
+---------------------------------------------------------+
| ACOS(1)                                                 |
+---------------------------------------------------------+
| 0.000000                                                |
+---------------------------------------------------------+
1 row in set (0.00 sec)
ASIN(X)

The ASIN() function returns the arcsine of X. The value of X must be in the range of .1 to 1 or NULL is returned.

mysql> SELECT ASIN(1);
+---------------------------------------------------------+
| ASIN(1)                                                 |
+---------------------------------------------------------+
| 1.5707963267949                                         |
+---------------------------------------------------------+
1 row in set (0.00 sec)
ATAN(X)

This function returns the arctangent of X.

mysql> SELECT ATAN(1);
+---------------------------------------------------------+
| ATAN(1)                                                 |
+---------------------------------------------------------+
| 0.78539816339745                                        |
+---------------------------------------------------------+
1 row in set (0.00 sec)
ATAN2(Y,X)

This function returns the arctangent of the two arguments: X and Y. It is similar to the arctangent of Y/X, except that the signs of both are used to find the quadrant of the result.

mysql> SELECT ATAN2(3,6);
+---------------------------------------------------------+
| ATAN2(3,6)                                              |
+---------------------------------------------------------+
| 0.46364760900081                                        |
+---------------------------------------------------------+
1 row in set (0.00 sec)
BIT_AND(expression)

The BIT_AND function returns the bitwise AND of all bits in expression. The basic premise is that if two corresponding bits are the same, then a bitwise AND operation will return 1, while if they are different, a bitwise AND operation will return 0. The function itself returns a 64-bit integer value. If there are no matches, then it will return 18446744073709551615. The following example performs the BIT_AND function on the PRICE column grouped by the MAKER of the car:

mysql> SELECT 
          MAKER, BIT_AND(PRICE) BITS
          FROM CARS GROUP BY MAKER
+---------------------------------------------------------+
|MAKER           BITS                                     |
+---------------------------------------------------------+
|CHRYSLER        512                                      |
|FORD            12488                                    |
|HONDA           2144                                     |
+---------------------------------------------------------+
1 row in set (0.00 sec)
BIT_COUNT(numeric_value)

The BIT_COUNT() function returns the number of bits that are active in numeric_value. The following example demonstrates using the BIT_COUNT() function to return the number of active bits for a range of numbers:

mysql> SELECT
          BIT_COUNT(2) AS TWO,
          BIT_COUNT(4) AS FOUR,
          BIT_COUNT(7) AS SEVEN
+-----+------+-------+
| TWO | FOUR | SEVEN |
+-----+------+-------+
|   1 |    1 |     3 |
+-----+------+-------+
1 row in set (0.00 sec)
BIT_OR(expression)

The BIT_OR() function returns the bitwise OR of all the bits in expression. The basic premise of the bitwise OR function is that it returns 0 if the corresponding bits match, and 1 if they do not. The function returns a 64-bit integer, and, if there are no matching rows, then it returns 0. The following example performs the BIT_OR() function on the PRICE column of the CARS table, grouped by the MAKER:

mysql> SELECT 
          MAKER, BIT_OR(PRICE) BITS
          FROM CARS GROUP BY MAKER
+---------------------------------------------------------+
|MAKER           BITS                                     |
+---------------------------------------------------------+
|CHRYSLER        62293                                    |
|FORD            16127                                    |
|HONDA           32766                                    |
+---------------------------------------------------------+
1 row in set (0.00 sec)
CEIL(X)

CEILING(X)

These function return the smallest integer value that is not smaller than X. Consider the following example:

mysql> SELECT CEILING(3.46);
+---------------------------------------------------------+
| CEILING(3.46)                                           |
+---------------------------------------------------------+
| 4                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT CEIL(-6.43);
+---------------------------------------------------------+
| CEIL(-6.43)                                             |
+---------------------------------------------------------+
| -6                                                      |
+---------------------------------------------------------+
1 row in set (0.00 sec)
CONV(N,from_base,to_base)

The purpose of the CONV() function is to convert numbers between different number bases. The function returns a string of the value N converted from from_base to to_base. The minimum base value is 2 and the maximum is 36. If any of the arguments are NULL, then the function returns NULL. Consider the following example, which converts the number 5 from base 16 to base 2:

mysql> SELECT CONV(5,16,2);
+---------------------------------------------------------+
| CONV(5,16,2)                                            |
+---------------------------------------------------------+
| 101                                                     |
+---------------------------------------------------------+
1 row in set (0.00 sec)
COS(X)

This function returns the cosine of X. The value of X is given in radians.

mysql>SELECT COS(90);
+---------------------------------------------------------+
| COS(90)                                                 |
+---------------------------------------------------------+
| -0.44807361612917                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)
COT(X)

This function returns the cotangent of X. Consider the following example:

mysql>SELECT COT(1);
+---------------------------------------------------------+
| COT(1)                                                  |
+---------------------------------------------------------+
| 0.64209261593433                                        |
+---------------------------------------------------------+
1 row in set (0.00 sec)
DEGREES(X)

This function returns the value of X converted from radians to degrees.

mysql>SELECT DEGREES(PI());
+---------------------------------------------------------+
| DEGREES(PI())                                           |
+---------------------------------------------------------+
| 180.000000                                              |
+---------------------------------------------------------+
1 row in set (0.00 sec)
EXP(X)

This function returns the value of e (the base of the natural logarithm) raised to the power of X.

mysql>SELECT EXP(3);
+---------------------------------------------------------+
| EXP(3)                                                  |
+---------------------------------------------------------+
| 20.085537                                               |
+---------------------------------------------------------+
1 row in set (0.00 sec)
FLOOR(X)

This function returns the largest integer value that is not greater than X.

mysql>SELECT FLOOR(7.55);
+---------------------------------------------------------+
| FLOOR(7.55)                                             |
+---------------------------------------------------------+
| 7                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)
FORMAT(X,D)

The FORMAT() function is used to format the number X in the following format: ###,###,###.## truncated to D decimal places. The following example demonstrates the use and output of the FORMAT() function:

mysql>SELECT FORMAT(423423234.65434453,2);
+---------------------------------------------------------+
| FORMAT(423423234.65434453,2)                            |
+---------------------------------------------------------+
| 423,423,234.65                                          |
+---------------------------------------------------------+
1 row in set (0.00 sec)
GREATEST(n1,n2,n3,..........)

The GREATEST() function returns the greatest value in the set of input parameters (n1, n2, n3, a nd so on). The following example uses the GREATEST() function to return the largest number from a set of numeric values:

mysql>SELECT GREATEST(3,5,1,8,33,99,34,55,67,43);
+---------------------------------------------------------+
| GREATEST(3,5,1,8,33,99,34,55,67,43)                     |
+---------------------------------------------------------+
| 99                                                      |
+---------------------------------------------------------+
1 row in set (0.00 sec)
INTERVAL(N,N1,N2,N3,..........)

The INTERVAL() function compares the value of N to the value list (N1, N2, N3, and so on ). The function returns 0 if N < N1, 1 if N < N2, 2 if N <N3, and so on. It will return .1 if N is NULL. The value list must be in the form N1 < N2 < N3 in order to work properly. The following code is a simple example of how the INTERVAL() function works:

mysql>SELECT INTERVAL(6,1,2,3,4,5,6,7,8,9,10);
+---------------------------------------------------------+
| INTERVAL(6,1,2,3,4,5,6,7,8,9,10)                        |
+---------------------------------------------------------+
| 6                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)
INTERVAL(N,N1,N2,N3,..........)

The INTERVAL() function compares the value of N to the value list (N1, N2, N3, and so on ). The function returns 0 if N < N1, 1 if N < N2, 2 if N <N3, and so on. It will return .1 if N is NULL. The value list must be in the form N1 < N2 < N3 in order to work properly. The following code is a simple example of how the INTERVAL() function works:

mysql>SELECT INTERVAL(6,1,2,3,4,5,6,7,8,9,10);
+---------------------------------------------------------+
| INTERVAL(6,1,2,3,4,5,6,7,8,9,10)                        |
+---------------------------------------------------------+
| 6                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)
Remember that 6 is the zero-based index in the value list of the first value that was greater than N. In our case, 7 was the offending value and is located in the sixth index slot.

LEAST(N1,N2,N3,N4,......)

The LEAST() function is the opposite of the GREATEST() function. Its purpose is to return the least-valued item from the value list (N1, N2, N3, and so on). The following example shows the proper usage and output for the LEAST() function:

mysql>SELECT LEAST(3,5,1,8,33,99,34,55,67,43);
+---------------------------------------------------------+
| LEAST(3,5,1,8,33,99,34,55,67,43)                        |
+---------------------------------------------------------+
| 1                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)
LOG(X)

LOG(B,X)

The single argument version of the function will return the natural logarithm of X. If it is called with two arguments, it returns the logarithm of X for an arbitrary base B. Consider the following example:

mysql>SELECT LOG(45);
+---------------------------------------------------------+
| LOG(45)                                                 |
+---------------------------------------------------------+
| 3.806662                                                |
+---------------------------------------------------------+
1 row in set (0.00 sec)

mysql>SELECT LOG(2,65536);
+---------------------------------------------------------+
| LOG(2,65536)                                            |
+---------------------------------------------------------+
| 16.000000                                               |
+---------------------------------------------------------+
1 row in set (0.00 sec)
LOG10(X)

This function returns the base-10 logarithm of X.

mysql>SELECT LOG10(100);
+---------------------------------------------------------+
| LOG10(100)                                              |
+---------------------------------------------------------+
| 2.000000                                                |
+---------------------------------------------------------+
1 row in set (0.00 sec)
MOD(N,M)

This function returns the remainder of N divided by M. Consider the following example:

mysql>SELECT MOD(29,3);
+---------------------------------------------------------+
| MOD(29,3)                                               |
+---------------------------------------------------------+
| 2                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)
OCT(N)

The OCT() function returns the string representation of the octal number N. This is equivalent to using CONV(N,10,8).

mysql>SELECT OCT(12);
+---------------------------------------------------------+
| OCT(12)                                                 |
+---------------------------------------------------------+
| 14                                                      |
+---------------------------------------------------------+
1 row in set (0.00 sec)
PI()

This function simply returns the value of pi. MySQL internally stores the full double-precision value of pi.

mysql>SELECT PI();
+---------------------------------------------------------+
| PI()                                                    |
+---------------------------------------------------------+
| 3.141593                                                |
+---------------------------------------------------------+
1 row in set (0.00 sec)
POW(X,Y)

POWER(X,Y)

These two functions return the value of X raised to the power of Y.

mysql> SELECT POWER(3,3);
+---------------------------------------------------------+
| POWER(3,3)                                              |
+---------------------------------------------------------+
| 27                                                      |
+---------------------------------------------------------+
1 row in set (0.00 sec)
RADIANS(X)

This function returns the value of X, converted from degrees to radians.

mysql>SELECT RADIANS(90);
+---------------------------------------------------------+
| RADIANS(90)                                             |
+---------------------------------------------------------+
|1.570796                                                 |
+---------------------------------------------------------+
1 row in set (0.00 sec)
ROUND(X)

ROUND(X,D)

This function returns X rounded to the nearest integer. If a second argument, D, is supplied, then the function returns X rounded to D decimal places. D must be positive or all digits to the right of the decimal point will be removed. Consider the following example:

mysql>SELECT ROUND(5.693893);
+---------------------------------------------------------+
| ROUND(5.693893)                                         |
+---------------------------------------------------------+
| 6                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)

mysql>SELECT ROUND(5.693893,2);
+---------------------------------------------------------+
| ROUND(5.693893,2)                                       |
+---------------------------------------------------------+
| 5.69                                                    |
+---------------------------------------------------------+
1 row in set (0.00 sec)
SIGN(X)

This function returns the sign of X (negative, zero, or positive) as .1, 0, or 1.

mysql>SELECT SIGN(-4.65);
+---------------------------------------------------------+
| SIGN(-4.65)                                             |
+---------------------------------------------------------+
| -1                                                      |
+---------------------------------------------------------+
1 row in set (0.00 sec)

mysql>SELECT SIGN(0);
+---------------------------------------------------------+
| SIGN(0)                                                 |
+---------------------------------------------------------+
| 0                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)

mysql>SELECT SIGN(4.65);
+---------------------------------------------------------+
| SIGN(4.65)                                              |
+---------------------------------------------------------+
| 1                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)
SIN(X)

This function returns the sine of X. Consider the following example:

mysql>SELECT SIN(90);
+---------------------------------------------------------+
| SIN(90)                                                 |
+---------------------------------------------------------+
| 0.893997                                                |
+---------------------------------------------------------+
1 row in set (0.00 sec)
SQRT(X)

This function returns the non-negative square root of X. Consider the following example:

mysql>SELECT SQRT(49);
+---------------------------------------------------------+
| SQRT(49)                                                |
+---------------------------------------------------------+
| 7                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)
STD(expression)

STDDEV(expression)

The STD() function is used to return the standard deviation of expression. This is equivalent to taking the square root of the VARIANCE() of expression. The following example computes the standard deviation of the PRICE column in our CARS table:

mysql>SELECT STD(PRICE) STD_DEVIATION FROM CARS;
+---------------------------------------------------------+
| STD_DEVIATION                                           |
+---------------------------------------------------------+
| 7650.2146                                               |
+---------------------------------------------------------+
1 row in set (0.00 sec)
TAN(X)

This function returns the tangent of the argument X, which is expressed in radians.

mysql>SELECT TAN(45);
+---------------------------------------------------------+
| TAN(45)                                                 |
+---------------------------------------------------------+
| 1.619775                                                |
+---------------------------------------------------------+
1 row in set (0.00 sec)
TRUNCATE(X,D)

This function is used to return the value of X truncated to D number of decimal places. If D is 0, then the decimal point is removed. If D is negative, then D number of values in the integer part of the value is truncated. Consider the following example:

mysql>SELECT TRUNCATE(7.536432,2);
+---------------------------------------------------------+
| TRUNCATE(7.536432,2)                                    |
+---------------------------------------------------------+
| 7.53                                                    |
+---------------------------------------------------------+
1 row in set (0.00 sec)
分享到:
评论

相关推荐

    《Oracle Database编程指南》13-02:数值函数(Numeric Functions)

    数值函数(Numeric Functions)接受数字输入并返回数字值。大多数数值函数返回精确到38位小数的数值。超越函数COS、COSH、EXP、LN、LOG、SIN、SINH、SQRT、TAN和TANH精确到36位小数。超越函数ACOS、ASIN、ATAN和ATAN...

    MySql存储过程编程.chm

    Numeric Functions Section 9.3. Date and Time Functions Section 9.4. Other Functions Section 9.5. Conclusion Chapter 10. Stored Functions Section 10.1. Creating Stored Functions Section ...

    从数据类型 nvarchar 转换为 numeric 时出错.pdf

    在本案例中,从数据类型nvarchar转换为numeric时出现错误。这是因为在进行数据类型转换时,数据的内容需要符合目标数据类型的要求。例如,nvarchar是用于存储字符串的,而numeric是用于存储数字的。如果nvarchar类型...

    MySQL 8.0 Reference Manual MySQL 8.0 Reference Manual

    * Support for multiple data types, including numeric, string, date, and time * Support for indexing, including B-tree and hash indexing * Support for full-text searching and spatial indexing MySQL ...

    Chinese Std GBT7714 (numeric)

    “Chinese Std GBT7714 (numeric)”是一个针对Endnote用户的引用输出格式,专门设计用于遵循GB/T 7714的顺序编码制。这种引用格式的特点是按照参考文献在文本中出现的顺序进行编号,并且每个引用都有一组有序的数字...

    SQL中NUMERIC和DECIMAL的区别

    ### SQL中NUMERIC和DECIMAL的区别 在数据库设计与开发过程中,选择合适的数据类型是非常重要的一步,这直接关系到数据的存储效率与查询性能。在SQL语言中,`NUMERIC`和`DECIMAL`是最常用的数据类型之一,尤其是在...

    前端项目-numeric.zip

    【前端项目-numeric.zip】是一个专门针对JavaScript环境的数值分析项目,名为"Numeric"。这个项目主要用于在Web前端实现各种数值计算和分析功能,利用JavaScript的灵活性和浏览器的执行环境,为开发者提供了一套强大...

    Chinese Std GBT7714 (numeric) Copy.ens

    《Chinese Std GBT7714 (numeric) Copy.ens》文件详解 在信息技术领域,文件格式是数据存储和交换的基础。"Chinese Std GBT7714 (numeric) Copy.ens"是一个特定类型的文件,它涉及到文本处理、文献引用格式和语言...

    pgsql数据库转mysql数据库步骤及注意.docx

    * PgSQL 数据库中 numeric(12,0) 类型的主键,在转换之后,软件会自动给替换成 double(12,0)。 * PgSQL 数据库中 timestamp 是带毫秒值的,转到 MySQL 之后,如果继续想要毫秒值,要换成 timestamp(3),如果不再要...

    纯js实现人脸识别numeric.js

    纯js实现人脸识别numeric.js纯js实现人脸识别numeric.js纯js实现人脸识别numeric.js纯js实现人脸识别numeric.js

    numeric recipes fortran77 code

    《数值方法Numeric Recipes Fortran77源代码》是科学计算领域一本非常经典的著作,它为理解和应用数值计算方法提供了丰富的示例和实现。Fortran77是早期的面向过程的编程语言,尤其在科学计算中有着广泛的应用。本书...

    MYSQL基础入门pdf

    - **定点数类型**:DECIMAL和NUMERIC。 - **日期和时间类型**:DATE、TIME、DATETIME、TIMESTAMP、YEAR。 - **字符串类型**:CHAR、VARCHAR、TEXT、BLOB等。 - **二进制类型**:BINARY、VARBINARY、BLOB等。 ...

    Chinese Std GBT7714 (numeric).ens

    Chinese Std GBT7714 (numeric).ens

    人大金仓数据库适配mysql

    SELECT field_name + 0.0 AS numeric_field FROM table_name; ``` #### 三、其他注意事项 **1. 大小写敏感** - KingbaseES默认使用大小写敏感模式,因此所有列名都应转换为大写或小写,避免大小写混用导致识别...

    Endnote Chinese Std GBT7714 (author-year+numeric) (官网原版 )

    numeric格式: [1] 李赟铎, 宫恩浩, 李睿, et al. 深度学习技术与医学影像——现状及未来 [J]. 2018, 52(005): 321-6. author-year格式: 李赟铎, 宫恩浩, 李睿, et al. 2018. 深度学习技术与医学影像——现状及...

    MYSQL完全中文教程

    - 数值类型:整型(TINYINT, SMALLINT, MEDIUMINT, INTEGER, BIGINT)、浮点型(FLOAT, DOUBLE)和定点数(DECIMAL, NUMERIC)。 - 字符串类型:VARCHAR、CHAR、TEXT、BLOB。 - 日期和时间类型:DATE、TIME、...

    mysql 教程MYSQL5.0的安装

    在MySQL5.0中,数值类型包括TINYINT、SMALLINT、MEDIUMINT、INT、INTEGER、BIGINT、FLOAT、DOUBLE、DOUBLE PRECISION、REAL、DECIMAL和NUMERIC,每种类型都有不同的存储需求和适用场景。例如,TINYINT适合存储单字节...

    数值算法 numeric recipes 中的Pascal 程序 ,V1.3

    此为 与 数值算法 numeric recipes 一书相配的 Pascal 程序,Version1.3。 对于使用Pascal语言进行数值计算的朋友很有用。

    MYSQL安装包官方试用版

    MYSQL_TYPE_TINY TINYINT字段 MYSQL_TYPE_SHORT SMALLINT字段 MYSQL_TYPE_LONG INTEGER字段 MYSQL_TYPE_INT24 MEDIUMINT字段 MYSQL_TYPE_LONGLONG BIGINT字段 MYSQL_TYPE_DECIMAL DECIMAL或NUMERIC...

Global site tag (gtag.js) - Google Analytics