MySQL函数---数学函数

(1)绝对值函数abs(x)和返回圆周率函数PI()。
(2)平方根函数sqrt(x)和求余函数mod(x,y)。
(3)获取整数的函数ceil(x)、ceiling(x)和floor(x)。
(4)获取随机数的函数rand()和rand(x)。
(5)函数round(x)、round(x,y)和truncate(x,y)。
(6)符号函数sign(x)。
(7)幂函数运算pow(x,y)、power(x,y)、exp(x)。
(8)对数运算函数log(x)和log10(x)。
(9)角度与弧度相互转换的函数ranians(x)和degrees(x)。
(10)正弦函数sin(x)和反正弦函数asin(x)。
(11)余弦函数cos(x)和反余弦函数acos(x)。
(12)正切函数、反正切函数和余切函数。


(1)绝对值函数abs(x)和返回圆周率函数PI()。

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)

(2)平方根函数sqrt(x)和求余函数mod(x,y)。

1.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)

2.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)

(3)获取整数的函数ceil(x)、ceiling(x)和floor(x)。

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

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

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

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

(4)获取随机数的函数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)

(5)函数round(x)、round(x,y)和truncate(x,y)。

1.round(x),对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)

2.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)

(6)符号函数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)

(7)幂函数运算pow(x,y)、power(x,y)、exp(x)。

1.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)

2.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)

(8)对数运算函数log(x)和log10(x)。

1.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)

2.log10(x)
【例】使用log10计算以10为基数的对数,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)

(9)角度与弧度相互转换的函数radians(x)和degrees(x)。

1.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)

2.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)

(10)正弦函数sin(x)和反正弦函数asin(x)。

1.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)

2.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)

(11)余弦函数cos(x)和反余弦函数acos(x)。

1.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)

2.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)

(12)正切函数、反正切函数和余切函数。

1.正切函数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)

2.反正切函数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)

3.余切函数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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hudie.

不要打赏!不要打赏!不要打赏!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值