MySQL数学函数

官方文档:Numeric Functions and Operators

NameDescription
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
DIV Integer division
/ Division operator
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() Return the base-10 logarithm of the argument
LOG2() Return the base-2 logarithm of the argument
- Minus operator
MOD() Return the remainder
%MOD Modulo operator
PI() Return the value of pi
+ Addition operator
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
* Multiplication operator
TRUNCATE() Truncate to specified number of decimal places
- Change the sign of the argument

1.ABS

ABS(x)

返回x的绝对值

2.ACOS/ASIN/ATAN

ACOS(X)/ASIN(X):返回反余弦/正弦的值。如果X不在[-1,1]之间,则返回NULL。

ATAN(X):返回反正切的值。

3.CEILING/FLOOR

CEILING(x):返回大于x的最小整数值

FLOOR(x):返回小于x的最大整数值

4.CONV

CONV(N,from_base,to_base) 
在不同的数字基之间变换数字。返回数字N的字符串数字,从from_base基变换为to_base基,如果任何参数是NULL,返回NULL。参数N解释为一个整数,但是可以指定为一个整数或一个字符串。最小基是2且最大的基是36。如果to_base是一个负数,N被认为是一个有符号数,否则,N被当作无符号数。 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'+0xa,10,10);
-> '40'

5.COS/COT/SIN/TAN

COS(X)/SIN(X):余弦/正弦。

COT(X)/TAN(X):余切/正切。

6.CRC32

CRC32(str):返回str的循环冗余码(32位的无符号整数)。如果str为NULL,返回NULL。

7.DEGREES/RADIANS

DEGREES(X):将弧度转换为角度。

RADIANS(X):将角度转换为弧度。

8.EXP

EXP(x)

返回值e(自然对数的底)的x次方

9.GREAST/LEAST

GREATEST(value1,value2,...)/LEAST(value1,value2,...):返回几个数中的最大值/最小值。如果有参数为NULL,则返回NULL。

mysql> SELECT GREATEST(2,0);
        -> 2
mysql> SELECT GREATEST(34.0,3.0,5.0,767.0);
        -> 767.0
mysql> SELECT GREATEST('B','A','C');
        -> 'C'

mysql> SELECT LEAST(2,0);
        -> 0
mysql> SELECT LEAST(34.0,3.0,5.0,767.0);
        -> 3.0
mysql> SELECT LEAST('B','A','C');
        -> 'A'

  

10.LN/LOG/LOG2/LOG10

LN(x):返回x的自然对数

LOG(X):等同于LN(X)

LOG(X,Y):返回x的以y为底的对数

LOG2(X):等同于LOG(2,X)

LOG10(X):等同于LOG(10,X)

11.MOD

MOD(x,y)

返回x/y的模(余数)

12.PI

PI()

返回pi的值(圆周率)

13.POW

POW(X,Y):返回X的Y次方。

14.RAND

RAND()/RAND(X)

返回0到1内的随机值,可以通过提供一个参数(种子)使RAND()随机数生成器生成一个指定的值。

15.ROUND

ROUND(x,y)

返回参数x的四舍五入的有y位小数的值

16.SIGN

SIGN(x)

返回代表数字x的符号的值。正数为1;0为0;负数为-1。

17.SQRT

SQRT(x)

返回一个数的平方根

18.TRUNCATE

TRUNCATE(X,D)

返回数字X截短为D位小数的结果。如果D大于小数位数,则返回原数;如果D为负数,则舍去小数,且将整数部分的后D位置为0。

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

  

 

 

原文地址:https://www.cnblogs.com/waterystone/p/5606119.html