大数据——MySQL数学函数

1、绝对值函数:abs(x)

【例】求2、-3.3和-33的绝对值,SQL语句如下:

mysql> select abs(2),
    -> abs(-3.3),
    -> abs(-33);
+--------+-----------+----------+
| abs(2) | abs(-3.3) | abs(-33) |
+--------+-----------+----------+
|      2 |       3.3 |       33 |
+--------+-----------+----------+
1 row in set (0.00 sec)

2、返回圆周率函数:PI()

【例】返回 圆周率的值,SQL语句如下:

mysql> select pi();
+----------+
| pi()     |
+----------+
| 3.141593 |
+----------+
1 row in set (0.00 sec)

3、平方根函数:sqrt(x)

【例】求9、40、-49的二次平方根,SQL语句如下:

mysql> select sqrt(9),
    -> sqrt(40),
    -> sqrt(-49);
+---------+-------------------+-----------+
| sqrt(9) | sqrt(40)          | sqrt(-49) |
+---------+-------------------+-----------+
|       3 | 6.324555320336759 |      NULL |
+---------+-------------------+-----------+
1 row in set (0.00 sec)

4、求余函数:mod(x,y)

【例】对mod(31,8)、mod(234,10)、mod(45.5,6)进行求余运算,SQL语句如下:

mysql> select mod(31,8),
    -> mod(234,10),
    -> mod(45.5,6);
+-----------+-------------+-------------+
| mod(31,8) | mod(234,10) | mod(45.5,6) |
+-----------+-------------+-------------+
|         7 |           4 |         3.5 |
+-----------+-------------+-------------+
1 row in set (0.00 sec)

5、获取整数的函数:ceil(x)、ceiling(x)和floor(x)

【例】使用ceil(x)、ceiling(x)函数向上取整,SQL语句如下:

mysql> select ceil(-3.35),ceiling(3.35);
+-------------+---------------+
| ceil(-3.35) | ceiling(3.35) |
+-------------+---------------+
|          -3 |             4 |
+-------------+---------------+
1 row in set (0.00 sec)

【例】使用floor(x)函数向下取整,SQL语句如下:

mysql> select floor (-3.35),floor(3.35);
+---------------+-------------+
| floor (-3.35) | floor(3.35) |
+---------------+-------------+
|            -4 |           3 |
+---------------+-------------+
1 row in set (0.00 sec)

6、获取随机数的函数:rand()和rand(x)

rand(x)返回一个随机浮点值,范围在0—1之间,若已指定了一个整数参数x,则它被用作种子值,用来产生重复序列。
【例】使用rand()函数和rand(x)函数产生随机数,SQL语句如下:

mysql> select rand(),
    -> rand(),
    -> rand(10),
    -> rand(10),
    -> rand(11);
+-------------------+---------------------+--------------------+--------------------+-------------------+
| rand()            | rand()              | rand(10)           | rand(10)           | rand(11)          |
+-------------------+---------------------+--------------------+--------------------+-------------------+
| 0.736099214047286 | 0.37941273523439906 | 0.6570515219653505 | 0.6570515219653505 | 0.907234631392392 |
+-------------------+---------------------+--------------------+--------------------+-------------------+
1 row in set (0.00 sec)

7、函数round(x)、round(x,y)和truncate(x,y)

round(x)
【例】使用round(x)函数对操作数进行四舍五入操作,SQL语句如下:

mysql> select round(-1.14),
    -> round(-1.67),
    -> round(1.14),
    -> round(1.66);
+--------------+--------------+-------------+-------------+
| round(-1.14) | round(-1.67) | round(1.14) | round(1.66) |
+--------------+--------------+-------------+-------------+
|           -1 |           -2 |           1 |           2 |
+--------------+--------------+-------------+-------------+
1 row in set (0.00 sec)

round(x,y),对x四舍五入保留y位。
若y为负数,保留x值到小数点左边y位。
【例】使用round(x,y)函数对操作数x进行四舍五入操作,结果保留小数点后面指定y位,SQL语句如下:

mysql> select round(1.38,1),
    -> round(1.38,0),
    -> round(232.38,-1),
    -> round(232.38,-2);
+---------------+---------------+------------------+------------------+
| round(1.38,1) | round(1.38,0) | round(232.38,-1) | round(232.38,-2) |
+---------------+---------------+------------------+------------------+
|           1.4 |             1 |              230 |              200 |
+---------------+---------------+------------------+------------------+
1 row in set (0.00 sec)

truncate(x,y),对x四舍五入保留y位。
若y为负数,保留x值到小数点左边y位。
【例】使用round(x,y)函数对操作数x进行四舍五入操作,结果保留小数点后面指定y位,SQL语句如下:

mysql> select truncate(1.38,1),
    -> truncate(1.38,0),
    -> truncate(232.38,-1),
    -> truncate(232.38,-2);
+------------------+------------------+---------------------+---------------------+
| truncate(1.38,1) | truncate(1.38,0) | truncate(232.38,-1) | truncate(232.38,-2) |
+------------------+------------------+---------------------+---------------------+
|              1.4 |                1 |                 230 |                 200 |
+------------------+------------------+---------------------+---------------------+
1 row in set (0.00 sec)

8、符号函数:sign(x)

sign(x)返回参数的符号,x的值为负、零或正时返回结果依次为-1,0,1。
【例】使用sign函数返回参数的符号,SQL语句如下:

mysql> select sign(-21),
    -> sign(0),
    -> sign(21);
+-----------+---------+----------+
| sign(-21) | sign(0) | sign(21) |
+-----------+---------+----------+
|        -1 |       0 |        1 |
+-----------+---------+----------+
1 row in set (0.05 sec)

9、幂函数运算:pow(x,y)、power(x,y)、exp(x)

POW(x,y)和POWER(x,y)。
【例】使用pow和power函数进行乘方运算,SQL语句如下:

mysql> select pow(2,2),
    -> power(2,2),
    -> pow(2,-2),
    -> power(2,-2);
+----------+------------+-----------+-------------+
| pow(2,2) | power(2,2) | pow(2,-2) | power(2,-2) |
+----------+------------+-----------+-------------+
|        4 |          4 |      0.25 |        0.25 |
+----------+------------+-----------+-------------+
1 row in set (0.00 sec)

EXO(x)
EXO(x)返回e的x次方乘方后的值。
【例】使用EXP函数计算e的乘方,SQL语句如下:

mysql> select exp(3),
    -> exp(-3),
    -> exp(0);
+--------------------+----------------------+--------+
| exp(3)             | exp(-3)              | exp(0) |
+--------------------+----------------------+--------+
| 20.085536923187668 | 0.049787068367863944 |      1 |
+--------------------+----------------------+--------+
1 row in set (0.02 sec)

10、对数运算函数:log(x)、log10(x)

log(x)
【例】使用log(x)函数计算自然对数,SQL语句如下:

mysql> select log(3),
    -> log(-3);
+--------------------+---------+
| log(3)             | log(-3) |
+--------------------+---------+
| 1.0986122886681098 |    NULL |
+--------------------+---------+
1 row in set, 1 warning (0.05 sec)

log10(x)
【例】使用log10(x)函数计算自然对数,SQL语句如下:

mysql> select log10(2),
    -> log10(100),
    -> log10(-100);
+--------------------+------------+-------------+
| log10(2)           | log10(100) | log10(-100) |
+--------------------+------------+-------------+
| 0.3010299956639812 |          2 |        NULL |
+--------------------+------------+-------------+
1 row in set, 1 warning (0.00 sec)

11、角度与弧度相互转换的函数:radians(x)、degrees(x)

radians(x)将参数x由角度转化为弧度。
【例】使用radians将角度转换为弧度

mysql> select radians(90),
    -> radians(180);
+--------------------+-------------------+
| radians(90)        | radians(180)      |
+--------------------+-------------------+
| 1.5707963267948966 | 3.141592653589793 |
+--------------------+-------------------+
1 row in set (0.00 sec)

degrees(x)
【例】使用degrees将弧度转化为角度,SQL语句如下:

mysql> select degrees(pi()),
    -> degrees(pi()/2);
+---------------+-----------------+
| degrees(pi()) | degrees(pi()/2) |
+---------------+-----------------+
|           180 |              90 |
+---------------+-----------------+
1 row in set (0.00 sec)

12、正弦函数sin(x)和反正弦函数asin(x)

sin(x)
【例】使用sin函数计算正弦值,SQL语句如下:

mysql> select sin(1),
    -> round(sin(pi()));
+--------------------+------------------+
| sin(1)             | round(sin(pi())) |
+--------------------+------------------+
| 0.8414709848078965 |                0 |
+--------------------+------------------+
1 row in set (0.05 sec)

asin(x)
【例】使用asin(x)函数计算反正弦值,SQL语句如下:

mysql> select asin(0.8414709848078965),asin(3);
+--------------------------+---------+
| asin(0.8414709848078965) | asin(3) |
+--------------------------+---------+
|                        1 |    NULL |
+--------------------------+---------+
1 row in set (0.00 sec)

13、余弦函数cos(x)和反余弦函数acos(x)

COS(x)
【例】使用cos函数计算余弦值,SQL语句如下:

mysql> select cos(0),cos(pi()),cos(1);
+--------+-----------+--------------------+
| cos(0) | cos(pi()) | cos(1)             |
+--------+-----------+--------------------+
|      1 |        -1 | 0.5403023058681398 |
+--------+-----------+--------------------+
1 row in set (0.00 sec)

反余弦函数:acos(x)
ACOS(x)
【例】使用acos(x)函数计算反余弦函数,SQL语句如下:

mysql> select acos(1),acos(0),round(acos(0.5403023058681398));
+---------+--------------------+---------------------------------+
| acos(1) | acos(0)            | round(acos(0.5403023058681398)) |
+---------+--------------------+---------------------------------+
|       0 | 1.5707963267948966 |                               1 |
+---------+--------------------+---------------------------------+
1 row in set (0.05 sec)

14、正切函数tan(x)、反正切函数atan(x)和余切函数cot(x)

正切函数TAN(x)
【例】使用tan函数计算正切值,SQL语句如下:

mysql> select tan(0.3),round(tan(pi()/4));
+---------------------+--------------------+
| tan(0.3)            | round(tan(pi()/4)) |
+---------------------+--------------------+
| 0.30933624960962325 |                  1 |
+---------------------+--------------------+
1 row in set (0.05 sec)

反正切函数ATAN(x)
【例】使用atan函数计算反正切值,SQL语句如下:

mysql> select atan(0.30933624960962325),atan(1);
+---------------------------+--------------------+
| atan(0.30933624960962325) | atan(1)            |
+---------------------------+--------------------+
|                       0.3 | 0.7853981633974483 |
+---------------------------+--------------------+
1 row in set (0.05 sec)

余切函数COT(x)
【例】使用cot()函数计算余切值,SQL语句如下:

mysql> select cot(0.3),1/tan(0.3),cot(pi()/4);
+--------------------+--------------------+--------------------+
| cot(0.3)           | 1/tan(0.3)         | cot(pi()/4)        |
+--------------------+--------------------+--------------------+
| 3.2327281437658275 | 3.2327281437658275 | 1.0000000000000002 |
+--------------------+--------------------+--------------------+
1 row in set (0.00 sec)
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值