当前位置:Gxlcms > 数据库问题 > 【MySQL函数】MySQL 5.5从零开始学第六章

【MySQL函数】MySQL 5.5从零开始学第六章

时间:2021-07-01 10:21:17 帮助过:13人阅读

1.1 绝对值函数ABS(x)

mysql> SELECT ABS(1),ABS(-1.1),ABS(-33);
+--------+-----------+----------+
| ABS(1) | ABS(-1.1) | ABS(-33) |
+--------+-----------+----------+
|      1 |       1.1 |       33 |
+--------+-----------+----------+
1 row in set (0.03 sec)

 1.2返回圆周率函数PI()

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

 1.3平方根函数SQRT(x)

 PS:SQRT(x)范围非负数x的二次方根

mysql> SELECT SQRT(9),SQRT(10),SQRT(-9);
+---------+--------------------+----------+
| SQRT(9) | SQRT(10)           | SQRT(-9) |
+---------+--------------------+----------+
|       3 | 3.1622776601683795 |     NULL |--负数没有平方根
+---------+--------------------+----------+
1 row in set (0.00 sec)

 1.4求余函数MOD(x,y)

mysql> select MOD(5,2),MOD(7,4),MOD(50,8);
+----------+----------+-----------+
| MOD(5,2) | MOD(7,4) | MOD(50,8) |
+----------+----------+-----------+
|        1 |        3 |         2 |
+----------+----------+-----------+
1 row in set (0.00 sec)

 1.5获取整数的函数CEIL(x)、CEILING(x)、FLOOR(x)

 PS:CEIL(x)、CEILING(x)意义相同

mysql> SELECT CEIL(-2.26),CEIL(2.26),CEILING(-2.26),CEILING(2.26);
+-------------+------------+----------------+---------------+
| CEIL(-2.26) | CEIL(2.26) | CEILING(-2.26) | CEILING(2.26) |
+-------------+------------+----------------+---------------+
|          -2 |          3 |             -2 |             3 |
+-------------+------------+----------------+---------------+
1 row in set (0.00 sec)

mysql> SELECT FLOOR(-2.26),FLOOR(2.26);
+--------------+-------------+
| FLOOR(-2.26) | FLOOR(2.26) |
+--------------+-------------+
|           -3 |           2 |
+--------------+-------------+
1 row in set (0.00 sec)

 1.6获取随机数的函数RAND()和RAND(x)

PS:a.RAND(x)返回一个随机浮点值v,范围在0到1之间。若已制定一个整数参数x,则它被用作种子值,用来产生重复序列。

  b.不带参数的RAND()每次产生的随机数值不同

  c.当RAND()参数相同时,将产生相同的随机数,不同的x产生的随机数值不同

mysql> SELECT RAND(),RAND(),RAND();
+--------------------+--------------------+--------------------+
| RAND()             | RAND()             | RAND()             |
+--------------------+--------------------+--------------------+
| 0.0917864060884215 | 0.6539400337766298 | 0.9943409813515293 |
+--------------------+--------------------+--------------------+
1 row in set (0.00 sec)

mysql> SELECT RAND(5),RAND(5),RAND(6);
+---------------------+---------------------+--------------------+
| RAND(5)             | RAND(5)             | RAND(6)            |
+---------------------+---------------------+--------------------+
| 0.40613597483014313 | 0.40613597483014313 | 0.6563190842571847 |
+---------------------+---------------------+--------------------+
1 row in set (0.00 sec)

 

【MySQL函数】MySQL 5.5从零开始学第六章

标签:时间函数   class   开始   入参   没有   不同   随机函数   绝对值   mod   

人气教程排行