【MySQL基础】02 数值函数

  • 目录
    • abs(x)
    • ceil(x)
    • floor(x)
    • mod(x,y)
    • rand()、rand(n)
    • round(x,[y])
    • truncate(x,y)

概览

abs(x)  返回x的绝对值
ceil(x) 返回大于x的最小整数
floor(x)    返回小于x的最大整数
mod(x,y)    返回x/y的模
rand()  返回0~1内的随机数
round(x,y)  返回参数x的四舍五入的有y位小数的值
truncate(x,y)   返回数字x截断y位小数的结果

详解

abs(x)

返回x的绝对值

mysql> select abs(-0.0),abs(-0.2),abs(2);
+-----------+-----------+--------+
| abs(-0.0) | abs(-0.2) | abs(2) |
+-----------+-----------+--------+
| 0         | 0.2       |      2 |
+-----------+-----------+--------+
1 row in set

ceil(x)

返回大于x的最小整数

mysql> select ceil(-0.15),ceil(1.5);
+-------------+-----------+
| ceil(-0.15) | ceil(1.5) |
+-------------+-----------+
|           0 |         2 |
+-------------+-----------+
1 row in set

floor(x)

返回小于x的最大整数

mysql> select floor(-0.15),floor(1.5);
+--------------+------------+
| floor(-0.15) | floor(1.5) |
+--------------+------------+
|           -1 |          1 |
+--------------+------------+
1 row in set

mod(x,y)

返回x/y的模,即取余

mysql> select mod(5,2),mod(5,0),mod(null,4),mod(null,null),mod(1,0.3);
+----------+----------+-------------+----------------+------------+
| mod(5,2) | mod(5,0) | mod(null,4) | mod(null,null) | mod(1,0.3) |
+----------+----------+-------------+----------------+------------+
|        1 | NULL     | NULL        | NULL           | 0.1        |
+----------+----------+-------------+----------------+------------+
1 row in set

rand()、rand(n)

rand():返回[0,1)之间的随机数(包括0,不包括1)。
rand(n):即有参数数,如rand(2),相当于指定随机数生产的种子,那么这种情况产生的随机数是可重复的。

mysql> select rand(),rand();
+--------------------+--------------------+
| rand()             | rand()             |
+--------------------+--------------------+
| 0.5091565535489065 | 0.7198327637462251 |
+--------------------+--------------------+
1 row in set

mysql> select rand(2),rand(3);
+--------------------+--------------------+
| rand(2)            | rand(3)            |
+--------------------+--------------------+
| 0.6555866465490187 | 0.9057697559760601 |
+--------------------+--------------------+
1 row in set

mysql> select rand(2),rand(3);
+--------------------+--------------------+
| rand(2)            | rand(3)            |
+--------------------+--------------------+
| 0.6555866465490187 | 0.9057697559760601 |
+--------------------+--------------------+
1 row in set

round(x,[y])

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

mysql> select round(1.5),round(1.446,1),round(1.456,1);
+------------+----------------+----------------+
| round(1.5) | round(1.446,1) | round(1.456,1) |
+------------+----------------+----------------+
| 2          | 1.4            | 1.5            |
+------------+----------------+----------------+
1 row in set

truncate(x,y)

返回数字x截断y位小数的结果

mysql> select truncate(1.246,2),truncate(1.2,3);
+-------------------+-----------------+
| truncate(1.246,2) | truncate(1.2,3) |
+-------------------+-----------------+
| 1.24              | 1.2             |
+-------------------+-----------------+
1 row in set


欢迎关注我的微信公众号:JAVA必知必会

  • 分享精品视频资源
  • 分享基础系列文档
【MySQL基础】02 数值函数_第1张图片
image

你可能感兴趣的:(【MySQL基础】02 数值函数)