MySQL常见数值函数整理
大虾好吃吗 人气:0绝对值函数
语法格式:ABS(X)
例:查看三个数值的绝对值(负的绝对值为它的正整数,0的绝对值为0,正的绝对值为它本身)。
mysql> select abs(2),abs(-32),abs(-0.5); +--------+----------+-----------+ | abs(2) | abs(-32) | abs(-0.5) | +--------+----------+-----------+ | 2 | 32 | 0.5 | +--------+----------+-----------+ 1 row in set (0.01 sec)
求余函数
语法格式:MOD(N,M), N % M, N MOD M
例:查看三个数值,三个方法查看取余。
mysql> select mod(10,3),20%3,15 mod 4; +-----------+------+----------+ | mod(10,3) | 20%3 | 15 mod 4 | +-----------+------+----------+ | 1 | 2 | 3 | +-----------+------+----------+ 1 row in set (0.00 sec)
获取整数的函数
语法格式:CEIL(X)、CEILING(x) 、FLOOR(x)
例:ceil和ceiling返回不小于x的最小整数,floor返回不大于x的最大整数。
mysql> select ceil(2.1234),ceiling(3.1234),floor(4.1234); +--------------+-----------------+---------------+ | ceil(2.1234) | ceiling(3.1234) | floor(4.1234) | +--------------+-----------------+---------------+ | 3 | 4 | 4 | +--------------+-----------------+---------------+ 1 row in set (0.00 sec)
获取随机数的函数
语法格式:RAND() 、RAND(x)
例1:rand()用于返回一个0~1之间的随机浮点数。
mysql> select rand(),rand(); +--------------------+---------------------+ | rand() | rand() | +--------------------+---------------------+ | 0.8802711524816893 | 0.21204793100436026 | +--------------------+---------------------+ 1 row in set (0.00 sec) 例2:rand(x)用于返回一个0~1之间的浮点数,x作为种子值用来产生重复序列。 mysql> select rand(2),rand(2),rand(1000000); +--------------------+--------------------+--------------------+ | rand(2) | rand(2) | rand(1000000) | +--------------------+--------------------+--------------------+ | 0.6555866465490187 | 0.6555866465490187 | 0.2644144708890603 | +--------------------+--------------------+--------------------+ 1 row in set (0.00 sec)
四舍五入的函数
语法格式:ROUND(x) 、ROUND(x,y)
例:round(x)四舍五入为整数,round(x,y)四舍五入保留后面两位小数。
mysql> select round(3.512),round(4.235,2); +--------------+----------------+ | round(3.512) | round(4.235,2) | +--------------+----------------+ | 4 | 4.24 | +--------------+----------------+ 1 row in set (0.00 sec)
截取数值的函数
语法格式:TRUNCATE(x,y)
例1:截取数值,保留y位。
mysql> select truncate(2.567,2),truncate(1.99,1); +-------------------+------------------+ | truncate(2.567,2) | truncate(1.99,1) | +-------------------+------------------+ | 2.56 | 1.9 | +-------------------+------------------+ 1 row in set (0.00 sec)
例2:截取1234为1000,截取1234为1200(负数表明把右侧的数值截取为0,看到下面的操作可以想到,把负数改为-1可以看到多少?结果为1230!)
mysql> select truncate(1234,-3),truncate(1234,-2); +-------------------+-------------------+ | truncate(1234,-3) | truncate(1234,-2) | +-------------------+-------------------+ | 1000 | 1200 | +-------------------+-------------------+ 1 row in set (0.00 sec)
符号函数
语法格式:SIGN(x)
例:sign(x)表示任何负数返回-1,0返回0,任何正数返回1。
mysql> select sign(-123),sign(0),sign(223); +------------+---------+-----------+ | sign(-123) | sign(0) | sign(223) | +------------+---------+-----------+ | -1 | 0 | 1 | +------------+---------+-----------+ 1 row in set (0.00 sec)
幂运算函数
语法格式:POW(x,y) 、POWER(x,y)
例:两个用处相同,pow为返回2的三次方,power返回3的四次方。
mysql> select pow(2,3),power(3,4); +----------+------------+ | pow(2,3) | power(3,4) | +----------+------------+ | 8 | 81 | +----------+------------+ 1 row in set (0.00 sec)
总结
加载全部内容