数值函数:
用来处理很多数值方面的运算,使用数值函数,可以免去很多繁杂的判断求值的过程,能够大大提高用户的工作效率。
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;
作者:点碎的阳光
原文链接:https://www.cnblogs.com/tianming18/p/10275388.html
共同学习,写下你的评论
评论加载中...
作者其他优质文章