数据库教程:MySQL常用数值函数

数值函数: 用来处理很多数值方面的运算,使用数值函数,可以免去很多繁杂的判断求值的过程,能够大大提高用户的工作效率。 1、ABS(x):返回 x 的绝对值 2、CEIL(x):返回不小于 x 的最小整数,也就是说得大于或等于x的最小整数 同义词:ceiling(x) 3、FLOOR(x):返回不大于 …

数值函数:

  用来处理很多数值方面的运算,使用数值函数,可以免去很多繁杂的判断求值的过程,能够大大提高用户的工作效率。

MySQL常用数值函数

1、abs(x):返回 x 的绝对值

mysql> select abs(-0.8),abs(0.8); +-----------+----------+ | abs(-0.8) | abs(0.8) | +-----------+----------+ |       0.8 |      0.8 | +-----------+----------+

 

2、ceil(x):返回不小于 x 的最小整数,也就是说得大于或等于x的最小整数

  同义词:ceiling(x)

mysql> select ceil(1); +---------+ | ceil(1) | +---------+ |       1 | +---------+  mysql> select ceil(1.23),ceiling(-1.23); +------------+----------------+ | ceil(1.23) | ceiling(-1.23) | +------------+----------------+ |          2 |             -1 | +------------+----------------+

 

3、floor(x):返回不大于 x 的最大整数(与ceil的用法刚好相反)

mysql> select floor(1.23),floor(-1.23); +-------------+--------------+ | floor(1.23) | floor(-1.23) | +-------------+--------------+ |           1 |           -2 | +-------------+--------------+

 

4、mod(x,y):返回数字x除以y后的余数:x mod y

  和 x%y 的结果相同;

  模数和被模数任何一个为null(无效数)结果都为 null

mysql> select mod(123,10),234%7,3 mod 2; +-------------+-------+---------+ | mod(123,10) | 234%7 | 3 mod 2 | +-------------+-------+---------+ |           3 |     3 |       1 | +-------------+-------+---------+

注意:余数可以有小数;除数为0不抛出异常

mysql> select mod(3.14,3),mod(3,0); +-------------+----------+ | mod(3.14,3) | mod(3,0) | +-------------+----------+ |        0.14 |     null | +-------------+----------+

 

5、round(x[,d]):将数字x四舍五入到指定的小数位数d

  ①如果不指定d,则默认为0

  ②如果d是负数,表示从小数点的左边进行四舍五入

mysql> select round(1.58),round(1.298,1); +-------------+----------------+ | round(1.58) | round(1.298,1) | +-------------+----------------+ |           2 |            1.3 | +-------------+----------------+  mysql> select round(1.58,0),round(1.298,-1); +---------------+-----------------+ | round(1.58,0) | round(1.298,-1) | +---------------+-----------------+ |             2 |               0 | +---------------+-----------------+

 

6、truncate(x,d):将数字x截断到指定的小数位数d(不四舍五入)

  ①如果d为0,表示不要小数

  ②如果d是负数,表示从小数点的左边进行截断

mysql> select truncate(1.999,1),truncate(1.999,0); +-------------------+-------------------+ | truncate(1.999,1) | truncate(1.999,0) | +-------------------+-------------------+ |               1.9 |                 1 | +-------------------+-------------------+  mysql> select truncate(-1.999,1),truncate(123,-2); +--------------------+------------------+ | truncate(-1.999,1) | truncate(123,-2) | +--------------------+------------------+ |               -1.9 |              100 | +--------------------+------------------+

注意:truncate 和 round 的区别在于 truncate 仅仅是截断,而不进行四舍五入

mysql> select round(1.235,2),truncate(1.235,2); +----------------+-------------------+ | round(1.235,2) | truncate(1.235,2) | +----------------+-------------------+ |           1.24 |              1.23 | +----------------+-------------------+

 

7、rand():返回一个随机浮点数v(0<=v<1.0)

mysql> select rand(),rand(); +--------------------+---------------------+ | rand()             | rand()              | +--------------------+---------------------+ | 0.7085628693071779 | 0.19879874978102627 | +--------------------+---------------------+

rand(x):指定整数x,则用作种子值,产生一个可重复的数字序列

mysql> select rand(1),rand(2),rand(1); +---------------------+--------------------+---------------------+ | rand(1)             | rand(2)            | rand(1)             | +---------------------+--------------------+---------------------+ | 0.40540353712197724 | 0.6555866465490187 | 0.40540353712197724 | +---------------------+--------------------+---------------------+

利用rand()函数可以取任意指定范围内的随机数

  类似于shell> $((random % 100))得到随机值  

比如:产生 0~100 内的任意随机整数

mysql> select ceil(100*rand()),ceil(100*rand()); +------------------+------------------+ | ceil(100*rand()) | ceil(100*rand()) | +------------------+------------------+ |               87 |               75 | +------------------+------------------+

 若要得到一个随机整数r,i <= r < j

expr:floor(i + rand() * (j – i))

  q:取随机整数r,7<=r<12

  a:mysql> select floor(7+(rand()*5));

注意:

  ①当在 where 子句中使用rand()时,每次当where执行时都要重新计算 rand()

  ②不能在order by子句中使用带有随机值的列

但是,可以以随机的顺序从表中检索行

  例如:mysql> select * from  players order by rand();

  ③order by rand()常和limit子句一起使用:

  例如:mysql> select * from table1,table2 where a=b and c<d  order by rand() limit 1000;

需要了解更多数据库技术:MySQL常用数值函数,都可以关注数据库技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/dtteaching/798703.html

(0)
上一篇 2021年9月12日
下一篇 2021年9月12日

精彩推荐